1.字典
是一种key-value的数据类型,使用就像字典
无序的因为无下标
创建一个字典:
info = {
‘stu1‘:‘qq‘,
‘stu2‘:‘ww‘,
‘stu3‘:‘ee‘,
}
print(info)
输出结果
{‘stu1‘: ‘qq‘, ‘stu2‘: ‘ww‘, ‘stu3‘: ‘ee‘}
1.1增改
有就修改,没有就增加
info[‘stu1‘] = "gg"
info[‘sut4‘] = ‘hhh‘
print(info)
{‘stu1‘: ‘gg‘, ‘stu2‘: ‘ww‘, ‘stu3‘: ‘ee‘, ‘sut4‘: ‘hhh‘}
1.2删除
del,pop()删除指定
del info[‘stu1‘]
print(info)
{‘stu2‘: ‘ww‘, ‘stu3‘: ‘ee‘, ‘sut4‘: ‘hhh‘}
info.pop(‘stu2‘)
print(info)
{‘stu3‘: ‘ee‘, ‘sut4‘: ‘hhh‘}
popitem()随机删除
info.popitem()
print(info)
{‘stu3‘: ‘ee‘}
1.3查询
get(‘keys’)查询key,有就返回value,如果没有就返回none
print(info.get(‘stu1‘))
1.4字典嵌套
values(),keys() 查询key和values
city = {
"北京":["东城","西城","大悦城"],
"上海":["虹桥","上海动物园","东方明珠"],
"浙江":["杭州","温州","横店"],
}
#打印values
print(city.values())
dict_values([[‘东城‘, ‘西城‘, ‘大悦城‘], [‘虹桥‘, ‘上海动物园‘, ‘东方明珠‘], [‘杭州‘, ‘温州‘, ‘横店‘]])
#打印key
print(city.keys())
dict_keys([‘北京‘, ‘上海‘, ‘浙江‘])
setdefault()方法-增
city.setdefault("USA",{"美国":["华盛顿","洛杉矶","环球影城"]})
print(city)
{‘北京‘: [‘东城‘, ‘西城‘, ‘大悦城‘], ‘上海‘: [‘虹桥‘, ‘上海动物园‘, ‘东方明珠‘], ‘浙江‘: [‘杭州‘, ‘温州‘, ‘横店‘], ‘USA‘: {‘美国‘: [‘华盛顿‘, ‘洛杉矶‘, ‘环球影城‘]}}
dir1.update(dir2)更新
info = {
‘stu1‘:‘qq‘,
‘stu2‘:‘ww‘,
‘stu3‘:‘ee‘,
}
b = {
‘stu1‘:‘qwe‘,
1:3,
2:5,
}
info.update(b)
print(info)
{‘stu1‘: ‘qwe‘, ‘stu2‘: ‘ww‘, ‘stu3‘: ‘ee‘, 1: 3, 2: 5}
items()字典转成列表
print(info.items())
dict_items([(‘stu1‘, ‘qq‘), (‘stu2‘, ‘ww‘), (‘stu3‘, ‘ee‘)])
fromkeys([1],”str”)初始化一个新的字典,每个value赋值相同
print(dict.fromkeys([6,7,8],"yrdy"))
{6: ‘yrdy‘, 7: ‘yrdy‘, 8: ‘yrdy‘}
修改用fromkeys初始化出来的字典其中的一层,都会跟着改
c = dict.fromkeys([6,7,8],[1,{"name":"wsy"},555])
print(c)
c[7][1][‘name‘] = "jack"
print(c)
{6: [1, {‘name‘: ‘wsy‘}, 555], 7: [1, {‘name‘: ‘wsy‘}, 555], 8: [1, {‘name‘: ‘wsy‘}, 555]}
{6: [1, {‘name‘: ‘jack‘}, 555], 7: [1, {‘name‘: ‘jack‘}, 555], 8: [1, {‘name‘: ‘jack‘}, 555]}
1.5字典的循环
city = {
"北京":["东城","西城","大悦城"],
"上海":["虹桥","上海动物园","东方明珠"],
"浙江":["杭州","温州","横店"],
}
for i in city: #高效
print(i,city[i])
for v,k in city.items(): #低效
print(v,k)
北京 [‘东城‘, ‘西城‘, ‘大悦城‘]
上海 [‘虹桥‘, ‘上海动物园‘, ‘东方明珠‘]
浙江 [‘杭州‘, ‘温州‘, ‘横店‘]
2.元组
只能查
列表元组互相转换
names = ("wsy","wwsy","jack")
p = list(names)
print(p)
[‘wsy‘, ‘wwsy‘, ‘jack‘]
转换回来
names = ("wsy","wwsy","jack")
p = list(names)
q = tuple(p)
print(q)
(‘wsy‘, ‘wwsy‘, ‘jack‘)
index方法-返回索引位置下标
names = ("wsy","wwsy","jack")
p = names.index("jack")
print(p)
2
count方法-搜索字符,返回个数
names = ("wsy","wwsy","jack")
p = names.count("wsy")
print(p)
1
3.集合
集合中只包含数字
list_1 = [1,4,5,7,3,6,7,9]
print(list_1)
list_1 = set(list_1)
list_2 =set([2,6,0,66,22,8,4])
print(list_1,list_2)
[1, 4, 5, 7, 3, 6, 7, 9]
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
intersection()方法-求交集
print(list_1.intersection(list_2))
print(list_1 & list_2)
union()方法-求并集
print(list_1.union(list_2))
print(list_2 | list_1)
difference()方法-求差集
#差集 in list_1 but not in list_2
print(list_1.difference(list_2))
print(list_2.difference(list_1))
判断是否是子集
list_3 = set([1,3,7])
print(list_3.issubset(list_1))
print(list_1.issuperset(list_3))
symmetric_difference()方法求对称差集
print(list_1.symmetric_difference(list_2))
print(list_1 ^ list_2)
pop()方法随机删除
print(list_1.pop())
原文地址:https://www.cnblogs.com/wsy1030/p/8990802.html