标签:python数据类型
一、str 字符串
字符串属于不可变类型,对字符串对象的进行的任何功能性操都不会改变字符串本身。
a.字符串常用功能:
1.lower 将所有大写变小写(功能不如casefold,仅支持英文)
name = ‘SMITH‘
v = name.lower()
print(v)
2. upper 将所有小写变大写
name = ‘smith‘
v = name.upper()
print(v)
3. startswith 检查是否以指定值开头
name = ‘smith‘
v = name.startswith(‘sm‘)
print(v)
4.endswith 检查是否以指定值结尾
name = ‘smith‘
v = name.endswith(‘h‘)
print(v)
5.find 找到指定子序列索引位置,不存在返回-1
name = ‘smith‘
v = name.find(‘i‘)
print(v)
6.strip 删除字符串两边的空格;lstrip 删除左边的空格;rstrip 删除右边的空格
name = ‘ al ex ‘
v = name.strip()
print(v)
7.split 对字符串进行分割
content = "nixhaoxma"
v = content.split(‘x‘)
print(v)
8. replace 对字符串进行替换
content = "NieoSBSmithSBZion"
v = content.replace(‘SB‘,‘->‘)
print(v)
9. join 元素拼接(元素字符串)
name = ‘smith‘
v = "/".join(name)
print(v)
b. 字符串其他功能
1. capitalize 字符串首字母大写
name = ‘smith‘
v = name.capitalize()
print(v)
2. casefold 将所有大写变小写(并且还支持其他语言,如:德语)
name = ‘SMITH‘
v = name.casefold()
print(v)
3.center 文本居中
name = ‘Smith‘
v = name.center(20,‘*‘)
print(v)
4.ljust\rjust 左右填充
name = ‘Smith‘
v1 = name.ljust(20,‘*‘)
v2 = name.rjust(20,‘*‘)
print(v1)
print(v2)
5. count 查看传入字符在字符串中出现的次数
参数1:要查找的值(子序列)
参数2:起始位置(索引)
参数3:结束位置(索引)
name = ‘jlksjlkkjalkjflkjdlkglkjlkgdsajlk‘
v = name.count(‘lk‘)
print(v)
v1 = name.count(‘k‘,8,-1)
print(v1)
6. expandtabs 找到制表符\t,进行替换(指定的长度包含值)
name = "sm\tith\teric"
v = name.expandtabs(20)
print(v)
7. index 找到指定子序列索引位置,不存在报错
name = ‘smith‘
v = name.index(‘t‘)
print(v)
v1 = name.index(‘x‘)
print(v1)
8. format\format_map 字符串格式化
tp1 = "我是:{0};年龄:{1};性别:{2}"
v = tp1.format("东方不败",18,"不知道")
print(v)
tp1 = "我是:{name};年龄:{age};性别:{gender}"
v = tp1.format(name=‘东方不败‘,age=18,gender=‘随意‘)
print(v)
tp1 = "我是:{name};年龄:{age};性别:{gender}"
v = tp1.format_map({‘name‘:‘东方不败‘,‘age‘:18,‘gender‘:‘女‘})
print(v)
9. isalpnum\isalpha 判断字符串是否是字母、汉字、数字
name = ‘alex123好‘
v = name.isalnum()
print(v)
name = ‘alex好‘
v = name.isalpha()
print(v)
10. isdecimal\isdigit\isnumeric 判断字符串是否是数字
num = ‘1234二②⑩‘
v1 = num.isdecimal() #‘1234‘
v2 = num.isdigit() #‘1234②⑩‘
v3 = num.isnumeric() #‘1234二②⑩‘
print(v1,v2,v3)
11. isidentifier 判断字符串是否是表示符(能否作为变量名)
n = ‘_name8‘
v = n.isidentifier()
print(v)
12. printable 判断字符串是否包含隐藏的字符(\t,\n 等)
name = "今天真热啊,\t好几天没下雨了"
v = name.isprintable()
print(v)
13. maketrans 创建字符映射的转换表(翻译)
n = str.maketrans(‘abcd‘,‘1234‘)
name = "abcdefghijklmnabcilniabcd"
v = name.translate(n)
print(v)
14. partition\rpartition 对字符串进行分割,保留分割的元素
content = "张三PQ李四PQ王五"
v = content.partition(‘PQ‘)
print(v)
v1 = content.rpartition(‘PQ‘)
print(v1)
15. swapspace 对字符串进行大小写转换
name = ‘Smith‘
v = name.swapcase()
print(v)
16. zfill 对字符串填充0
name = ‘nieo‘
v = name.zfill(20)
print(v)
二、int 数字
整数属于不可变类型,对整数对象的进行的任何功能性操都不会改变整数本身。
1. bit_length 当前整数的二进制表示,最少位数
age = 30
print(age.bit_length())
2.to_bytes 获取当前数据的字节表示
age = 30
v = age.to_bytes(10,byteorder=‘big‘)
print(v)
v1 = age.to_bytes(5,byteorder=‘little‘)
print(v1)
三、list 列表:
列表属于可变类型,对列表对象进行的功能性操作,会改变列表本身。
1. append 追加元素
user_list = [‘smith‘,‘zion‘,‘alex‘]
user_list.append(‘nike‘)
print(user_list)
2. remove 删除元素
user_list = [‘smith‘,‘zion‘,‘alex‘]
user_list.remove(‘zion‘)
print(user_list)
3. pop 删除并获取元素(索引方式)
user_list = [‘smith‘,‘zion‘,‘alex‘]
v = user_list.pop(2)
print(user_list)
print(v)
4. copy 拷贝(浅拷贝)
user_list = [‘smith‘,‘zion‘,‘alex‘]
v = user_list.copy()
print(v)
print(user_list)
5. count 计数
user_list = [‘smith‘,‘zion‘,‘alex‘,‘zion‘]
v = user_list.count(‘zion‘)
print(v)
6. extend 扩展原列表
user_list = [‘smith‘,‘zion‘,‘alex‘]
user_list.extend([‘nieo‘,‘nike‘,‘joe‘])
print(user_list)
7. reverse 反转列表
user_list = [‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘]
user_list.reverse()
print(user_list)
8. sort 排序(正向)
num = [20,30,40,21,23,33]
num.sort()
print(num)
name = [‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘]
name.sort()
print(name)
9. clear 清空
user_list = [‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘]
user_list.clear()
print(user_list)
列表的额外功能:
1. 切片
user_list = [‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘]
v = user_list[2]
print(v)
v1 = user_list[2:5:2]
print(v1)
user_list1 = [‘smith‘, ‘zion‘, ‘alex‘, [‘nieo‘,‘jack‘,‘jones‘], ‘nike‘, ‘joe‘]
v2 = user_list1[3][1:]
print(v2)
2. 长度(计算列表中的元素个数)
user_list1 = [‘smith‘, ‘zion‘, ‘alex‘, [‘nieo‘,‘jack‘,‘jones‘], ‘nike‘, ‘joe‘]
v = len(user_list1)
print(v)
3. del 删除
user_list1 = [‘smith‘, ‘zion‘, ‘alex‘, [‘nieo‘,‘jack‘,‘jones‘], ‘nike‘, ‘joe‘]
del user_list1[3][0:2] # 0 <= x <2
print(user_list1)
4. insert 插入元素
user_list1 = [‘smith‘, ‘zion‘, ‘alex‘, [‘nieo‘,‘jack‘,‘jones‘], ‘nike‘, ‘joe‘]
user_list1.insert(3,‘Tony‘)
print(user_list1)
四、tuple 元组:
元组可以看做是不可被修改的列表,属于不可变类型,对元组对象进行的功能性操作,不会改变元组本身。
1. count 获取元素个数
user_tuple = (‘smith‘, ‘zion‘, ‘alex‘, [‘nieo‘,‘jack‘,‘alex‘], ‘nike‘, ‘joe‘)
v = user_tuple.count(‘alex‘)
print(v)
2. index 获取元素的第一个索引位置
user_tuple = (‘smith‘, ‘zion‘, ‘alex‘, [‘nieo‘,‘jack‘,‘alex‘], ‘nike‘, ‘joe‘)
v = user_tuple.index(‘alex‘)
print(v)
元组的其他功能
1. 切片
user_tuple = (‘smith‘, ‘zion‘, ‘alex‘, [‘nieo‘, ‘jack‘, ‘alex‘], ‘nike‘, ‘joe‘)
v = user_tuple[3][1:]
print(v)
注:在填写元组内容时,需要在元组最后加逗号。
v = (‘joe‘,)
print(v)
五、dict 字典:
字典属于可变类型,对字典对象进行的功能性操作,会改变字典本身。字典可以嵌套,字典的key必须是不可变类型。
1. clear 清空字典
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
dic.clear()
print(dic)
2. copy 浅拷贝
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
v = dic.copy()
print(v)
3.get 根据key获取指定value,不存在不报错
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
v = dic.get(‘k111‘)
print(v)
4.pop 删除并获取对应的value值
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘}
v = dic.pop(‘k2‘)
print(dic)
print(v)
5. popitem 随机删除键值对,并获取到删除的键值
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘,‘k4‘:‘v4‘}
v = dic.popitem()
print(dic)
print(v)
6. setdefault 增加键值,如果存在则不做操作
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘,‘k4‘:‘v4‘}
dic.setdefault(‘k5‘,‘v5‘)
print(dic)
dic.setdefault(‘k1‘,‘ddfdg‘)
print(dic)
7. update 批量增加或修改
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘,‘k4‘:‘v4‘}
dic.update({‘k1‘:111,‘k7‘:777})
print(dic)
8. fromkeys 拷贝字典的键
dic = {‘k1‘:‘v1‘,‘k2‘:‘v2‘,‘k3‘:‘v3‘,‘k4‘:‘v4‘}
d1 = dic.fromkeys([‘k1‘,‘k2‘,‘k3‘],123)
d2 = dic.fromkeys([‘k2‘,‘k3‘,‘k4‘],[‘11‘,‘22‘,‘33‘])
print(dic)
print(d1)
print(d2)
六、set集合
集合可以看做是不可重复的列表,属于可变类型,对集合对象进行的功能性操作,会改变集合本身。
1. difference s1中存在,s2中不存在
s1 = {‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘}
s2 = {‘smith‘, ‘white‘, ‘blues‘, ‘jack‘, ‘cat‘, ‘joe‘}
v = s1.difference(s2)
print(v)
2. symmteric_difference 返回s1和s2中不重复的值
s1 = {‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘}
s2 = {‘smith‘, ‘white‘, ‘blues‘, ‘jack‘, ‘cat‘, ‘joe‘}
v = s1.symmetric_difference(s2)
print(v)
3. intersection 返回s1和s2的交集
s1 = {‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘}
s2 = {‘smith‘, ‘white‘, ‘blues‘, ‘jack‘, ‘cat‘, ‘joe‘}
v = s1.intersection(s2)
print(v)
4. union 返回s1和s2的并集
s1 = {‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘}
s2 = {‘smith‘, ‘white‘, ‘blues‘, ‘jack‘, ‘cat‘, ‘joe‘}
v = s1.union(s2)
print(v)
5.移除操作
s1 = {‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘}
s1.discard(‘alex‘)
print(s1)
6.update 批量更新,但不新增已存在的值
s1 = {‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘}
s1.update({‘alex‘,‘jone‘,‘jobs‘})
print(s1)
7. discard 移除操作
s1 = {‘smith‘, ‘zion‘, ‘alex‘, ‘nieo‘, ‘nike‘, ‘joe‘}
s1.discard(‘nike‘)
print(s1)
七、部分函数的用法:
a. range函数:
1.range函数在python 2中会直接生成所有的数字,在python 3中不会立即生成,只有在循环迭代时,才一个一个生成。python 2.7中的xrange和python 3 中的range功能相同。
for i in range(10,0,-2):
print(i)
2. range 循环显示列表中的元素
user_list = [‘smith‘,‘joe‘,‘jack‘]
for i in range(0,len(user_list)):
n = user_list[i]
print(n)
b. enumerate函数:
enumerate 额外生成一列有序的数字
user_list = [‘smith‘,‘joe‘,‘jack‘]
for i,ele in enumerate(user_list,1):
print(i,ele)
本文出自 “晴天” 博客,请务必保留此出处http://chenno.blog.51cto.com/979714/1923850
标签:python数据类型
原文地址:http://chenno.blog.51cto.com/979714/1923850