数据类型常用方法(字符串,列表,字典,集合)

1.字符串

.upper() 将字符串中的字母大写

s = 'alex44'
print(s.upper())
# ALEX44

.lower() 将字符串中的字母小写

s = 'GB2312'
print(s.lower())
# gb2312

.startswith() 判断字符串是否以xx开头,返回值为True或者False

s = 'alex999'
v = s.startswith('al')
print(v)
# True

.endswith() 判断字符串是否以xx结尾,返回值为True或者False

s = 'alex999'
v = s.endswith('al')
print(v)
# False

.strip() 去掉字符串两边的空格

s = '  放飞自我,  '
v = s.strip()
print(v)
# 放飞自我,

.split() 将字符串以xx分割开来并返回一个列表

s = 'alex,wusir,太白,GG'
v = s.split()
print(v)
# ['alex,wusir,太白,GG']

.join() 将字符串、列表、元组中的元素以指定的字符拼接成新的字符串

s = ('23','66','GG')
v = '_'.join(s)
print(v)
# 23_66_GG   注意:元素一定要是字符串格式

.replace() 将字符串中的元素换成指定元素

s = 'lllsien asidfawen   slidflek'
v = s.replace('l','u')
print(v)
# uuusien asidfawen   suidfuek

.encode() 将字符串以xx编码进行转换

s = '佩奇'
v = s.encode('gbk')
print(v)
# b'\xc5\xe5\xc6\xe6'

.format() 将字符串进行格式化输出

s = "{1}曰:学而时习之,不亦{0}。".format("说乎","孔子")
print(s)
# 孔子曰:学而时习之,不亦说乎。

.isdigit() 判断字符串是否是数字型的字符串,如果是,返回True;如果不是,返回False

a = input('请输入:')
if a.isdigit():
    print('输入的是数字')
else:
    print('请输入数字')

2.列表

.append() 在列表中追加元素(可以是任何数据类型)

lis1 = []
lis2 = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
for i in lis2:
    lis1.append(i)
print(lis1)
# [1, '22', True, None, [3, 4], ('GG', 4), {'name': 'GG', 'age': 18}, {5, 'hh'}]

.insert() 在列表中的指定位置添加元素

lis = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
lis.insert(4,'alex')
print(lis)
# [1, '22', True, None, 'alex', [3, 4], ('GG', 4), {'name': 'GG', 'age': 18}, {'hh', 5}]

.extend() 在列表的末尾添加另一个序列的元素(如果是字典的话,则添加键值)

lis = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
lis.extend({'name':'骗子','age':18,'gender':'性别'})
print(lis)
# [1, '22', True, None, [3, 4], ('GG', 4), {'name': 'GG', 'age': 18}, {5, 'hh'}, 'name', 'age', 'gender']

.pop() 去掉列表中的元素,默认是最后一个,可以在()中加索引删除指定元素

lis = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
lis.pop(5)
print(lis)
# [1, '22', True, None, [3, 4], {'name': 'GG', 'age': 18}, {'hh', 5}]

.remove() 去掉列表中的指定元素。()中为要删除的元素

lis = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
lis.remove([3,4])
print(lis)
# [1, '22', True, None, ('GG', 4), {'name': 'GG', 'age': 18}, {'hh', 5}]

.del 去掉列表中的元素,直接删除,不再保存。()用索引删除

lis = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
del lis[4][1]
print(lis)
# [1, '22', True, None, [3], ('GG', 4), {'name': 'GG', 'age': 18}, {'hh', 5}]

.clear() 删除列表中的所有元素

lis = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
lis.clear()
print(lis)
# []

.reverse() 将列表中元素进行反转。(只反转第一层)

lis = [1,'22',True, None,[3,4],('GG',4),{'name':'GG','age':18},{'hh',5}]
lis.reverse()
print(lis)
# [{5, 'hh'}, {'name': 'GG', 'age': 18}, ('GG', 4), [3, 4], None, True, '22', 1]

.sort() 将列表中的元素从小到大排列(默认)。可以设置sort(,reverse = true)

lis = [1,5,8,6,8,1,4,3,10,22,17]
lis.sort()
print(lis)
# [1, 1, 3, 4, 5, 6, 8, 8, 10, 17, 22]
lis.sort(reverse=True)
print(lis)
# [22, 17, 10, 8, 8, 6, 5, 4, 3, 1, 1]

3.字典

.keys() 获取字典中所有的键

info = {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]'}
for i in info.keys():
    print(i,end=' ')
# name pwd email 

.values() 获取字典中的所有值

info = {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]'}
for i in info.values():
    print(i,end=' ')
# wupeiqi oldboy [email protected] 

.items()

info = {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]'}
for i,j in info.items():
    print(i,j)
''' 
name wupeiqi
pwd oldboy
email [email protected]
'''

.update() 用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中。

info = {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]'}
info.update({'age':18}) #添加一个新的键值对
print(info)
# {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]', 'age': 18}

info.update(five=5,six=6) #传入关键字
print(info)
# {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]', 'five': 5, 'six': 6}

info.update([(8,9),('22','ui'),('sldkf','lskdf')]) #传入含元组或列表的列表(字典不能掺和到列表元组中)
print(info)
# {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]', 8: 9, '22': 'ui', 'sldkf': 'lskdf'}

info.update(name ='alex') #也可以直接改值
print(info)
# {'name': 'alex', 'pwd': 'oldboy', 'email': '[email protected]'}

.get() 返回指定键的值,如果值不在字典中返回默认值

dict.get(key, default=None)

info = {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]'}
a = info.get('name')
print(a)
# wupeiqi

b = info.get('aaa',666)
print(b)
# 666

.clear() 删除字典中所有的键值,返回None空值

info = {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]'}
a = info.clear()
print(a)
# None

.pop() 删除字典中指定的键值对,返回值

info = {'name': 'wupeiqi', 'pwd': 'oldboy', 'email': '[email protected]'}
a = info.pop('name')
print(info)
print(a)

# {'pwd': 'oldboy', 'email': '[email protected]'}
# wupeiqi

4.集合

.add() 在集合中加入元素,如果有重复元素,则不执行操作(添加的元素必须是可哈希元素)(列表,字典不能加入)

text = {'alex', 'Ray', 'Scout', 'iBoy', '22', 11, ('sss', 'pp')}
text.add(88)
print(text)
# {'22', 'alex', ('sss', 'pp'), 'Scout', 11, 'iBoy', 88, 'Ray'}

.discard() 删除集合中元素,如果没有该元素,则不进行操作。

text = {'alex', 'Ray', 'Scout', 'iBoy', '22', 11, ('sss', 'pp')}
text.discard('22')
print(text)
# {'Scout', 11, 'iBoy', 'Ray', ('sss', 'pp'), 'alex'}

.intersection() 交集

text1 = {'alex', 'Ray', 'Scout', 'iBoy', '22', 11, ('sss', 'pp')}
text2 = {'alex', 'Ray',88,('sss', 'pp')}
text = text1.intersection(text2)
print(text)
# {('sss', 'pp'), 'Ray', 'alex'}

.union() 并集

text1 = {'alex', 'Ray', 'Scout', 'iBoy', '22', 11, ('sss', 'pp')}
text2 = {'alex', 'Ray',88,('sss', 'pp')}
text = text1.union(text2)
print(text)
# {'Ray', 'alex', 'Scout', 11, '22', ('sss', 'pp'), 88, 'iBoy'}

.difference() 差集

text1 = {'alex', 'Ray', 'Scout', 'iBoy', '22', 11, ('sss', 'pp')}
text2 = {'alex', 'Ray',88,('sss', 'pp')}
text = text1.difference(text2)
print(text)
# {'iBoy', '22', 11, 'Scout'}

.symmetric-intersection() 对称差集

text1 = {'alex', 'Ray', 'Scout', 'iBoy', '22', 11, ('sss', 'pp')}
text2 = {'alex', 'Ray',88,('sss', 'pp')}
text = text1.symmetric_difference(text2)
print(text)
# {'Scout', 'iBoy', 11, 88, '22'}

猜你喜欢

转载自www.cnblogs.com/zcg921001/p/10678981.html