1. Dictionary
Is a key-value data type, used like a dictionary
Unordered because there is no subscript
Create a dictionary:
info = {
'stu1':'qq',
'stu2':'ww',
'stu3':'ee',
}
print(info)
output result
{'stu1': 'qq', 'stu2': 'ww', 'stu3': 'ee'}
1.1 Additions
Modify if there is, add if not
info['stu1'] = "gg"
info['sut4'] = 'hhh'print(info) {'stu1': 'gg', 'stu2': 'ww', 'stu3': 'ee', 'sut4': 'hhh'}
1.2 Delete
del,pop() deletes the specified
del info [ 'stu1' ]
print(info)
{'stu2': 'ww', 'stu3': 'ee', 'sut4': 'hhh'}
info.pop('stu2')print(info)
'stu3': 'ee', 'sut4': 'hhh'}
popitem() deletes randomly
info.popitem()
print(info)
{'stu3': 'ee'}
1.3 Query
get('keys') query key, return value if there is one, return none if not
print(info.get('stu1'))
1.4 Dictionary nesting
values(),keys() query keys and values
city = {
"Beijing":["East City","West City","Joy City"],
"Shanghai":["Hongqiao","Shanghai Zoo","Oriental Pearl"],
"Zhejiang":["Hangzhou" ,"Wenzhou","Hengdian"],
}
#打印values
print(city.values())
dict_values([['East City', 'West City', 'Joy City'], ['Hongqiao', 'Shanghai Zoo', 'Oriental Pearl'], ['Hangzhou', 'Wenzhou', 'Hengdian']])
#Print key
print(city.keys())
dict_keys(['Beijing', 'Shanghai', 'Zhejiang'])
setdefault() method - increase
city.setdefault("USA",{"USA":["Washington","Los Angeles","Universal Studios"]})
print(city)
{'Beijing': ['East City', 'West City', 'Joy City'], 'Shanghai': ['Hongqiao', 'Shanghai Zoo', 'Oriental Pearl'], 'Zhejiang': ['Hangzhou', ' Wenzhou', 'Hengdian'], 'USA': {'USA': ['Washington', 'Los Angeles', 'Universal Studio']}}
dir1.update(dir2) update
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() dictionary into list
print(info.items())
dict_items([('stu1', 'qq'), ('stu2', 'ww'), ('stu3', 'ee')])
fromkeys([1],"str") initializes a new dictionary with the same assignment for each value
print(dict.fromkeys([6,7,8],"yrdy"))
{6: 'yrdy', 7: 'yrdy', 8: 'yrdy'}
Modifying one layer of the dictionary initialized with fromkeys will follow the changes.
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 Looping over a dictionary
city = {
"Beijing":["East City","West City","Joy City"],
"Shanghai":["Hongqiao","Shanghai Zoo","Oriental Pearl"],
"Zhejiang":["Hangzhou" ,"Wenzhou","Hengdian"],
}
for i in city: #Efficient
print(i,city[i])
for v,k in city.items(): #Inefficient print
(v,k)
Beijing['East City', 'West City', 'Joy City']
Shanghai['Hongqiao', 'Shanghai Zoo', 'Oriental Pearl']
Zhejiang['Hangzhou', 'Wenzhou', 'Hengdian']
2. Tuples
only check
list-tuple conversion
names = ("wsy","wwsy","jack")
p = list(names)
print(p)
['wsy', 'wwsy', 'jack']
convert back
names = ("wsy","wwsy","jack")
p = list(names)
q = tuple(p)
print(q)
('wsy', 'wwsy', 'jack')
index method - returns the index position subscript
names = ("wsy","wwsy","jack")
p = names.index("jack")
print(p)
2
count method - searches for characters and returns the number
names = ("wsy","wwsy","jack")
p = names.count("wsy")
print(p)
1
3. Collection
The set contains only numbers
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() method - find intersection
print(list_1.intersection(list_2))
print(list_1 & list_2)
union() method - union
print(list_1.union(list_2))
print(list_2 | list_1)
difference() method - find the difference set
#差集 in list_1 but not in list_2
print(list_1.difference(list_2))
print(list_2.difference(list_1))
Determine whether it is a subset
list_3 = set([1,3,7])
print(list_3.issubset(list_1))
print(list_1.issuperset(list_3))
The symmetric_difference() method finds the symmetric difference set
print(list_1.symmetric_difference(list_2))
print(list_1 ^ list_2)
pop() method deletes randomly
print(list_1.pop())