1. Dictionaries
is a Key-value data type that is used like a dictionary
Unordered because there is no subscript
Create a dictionary:
info = {
' stu1 ':' QQ ',
' stu2 ':' ww ',
' stu3 ':' ee ',
}
Print(info)
Output results
{' stu1 ': ' QQ ', ' stu2 ': ' ww ', ' stu3 ': ' EE '}
1.1 Changes have been modified, there is no increase
info[ "GG"
info[ ' Sut4 ' ' hhh '
print (info)
{' stu1 ': ' GG ', ' stu2 ': ' ww ', ' stu3 ': ' ee ', ' sut4 ': ' HHH '}
1.2 DeleteDel,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 () Random deletion
Info.popitem ()
Print(info)
{' stu3 ': ' EE '}
1.3 QueriesGet (' Keys ') query key, there is return value, if not, return none
Print(info.get (' stu1 '))
Qq
1.4 Dictionary NestingVALUES (), keys () query key and values
City = {
"Beijing": ["Dongcheng", "Xicheng", "Great Joy City"],
"Shanghai": ["Hongqiao", "Shanghai Zoo", "Oriental Pearl"],
"Zhejiang": ["Hangzhou", "Wenzhou", "Hengdian"],
}
#打印values
Print (City.values ())
Dict_values ([' East Side ', ' West Side ', ' Grand Hyatt City '], [' Hongqiao ', ' Shanghai Zoo ', ' Oriental Pearl '], [' Hangzhou ', ' Wenzhou ', ' Hengdian ']]
#打印key
Print (City.keys ())
Dict_keys ([' Beijing ', ' Shanghai ', ' Zhejiang '])
SetDefault () Method-Increase
City.setdefault ("USA", {"United States": ["Washington", "Los Angeles", "Universal Studios"]})
Print (city)
{' Beijing ': [' Dongcheng ', ' Xicheng ', ' Grand Yue City ', ' Shanghai ': [' Hongqiao ', ' Shanghai Zoo ', ' Oriental Pearl '], ' Zhejiang ': [' Hangzhou ', ' ' Wenzhou ', ' Hengdian '], ' USA ': {' USA ': [' Washington ', ' Los Angeles ', ' Universal Studios ']} }
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}
The items () dictionary is turned into a list
Print (Info.items ())
Dict_items ([' stu1 ', ' QQ '), (' Stu2 ', ' ww '), (' Stu3 ', ' ee ')])
Fromkeys ([1], "str") Initializes a new dictionary, each value is assigned the same
Print (Dict.fromkeys ([6,7,8], "Yrdy"))
{6: ' Yrdy ', 7: ' Yrdy ', 8: ' Yrdy '}
Modify the dictionary initialized with Fromkeys one layer, will follow the change
c = dict.fromkeys ([6,7,8] ,[1,{ "Name" : "wsy" }, 555 print (c)
C[7 ][1][ ' name ' ] = Span lang= "en-us" > "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 Cycle of the dictionary
City = {
"Beijing": ["Dongcheng", "Xicheng", "Great Joy City"],
"Shanghai": ["Hongqiao", "Shanghai Zoo", "Oriental Pearl"],
"Zhejiang": ["Hangzhou", "Wenzhou", "Hengdian"],
}
For I in the city : #高效
print (I,city[i])
For v,k in City.items (): #低效
print (v,k)
Beijing [' Dongcheng ', ' Xicheng ', ' Great Joy City ']
Shanghai [' Hongqiao ', ' Shanghai Zoo ', ' Oriental Pearl ']
zhejiang [' Hangzhou ', ' Wenzhou ', ' Hengdian ']
2. Tuples
can only check
List tuples convert to each other
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-Search for characters, return number
Names = ("wsy","Wwsy","Jack")
p = names.count ("wsy")
Print(p)
1
3. CollectionThe collection 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-Finding the intersection
Print (List_1.intersection (list_2))
Print (List_1 & list_2)
Union () Method-Set
Print (List_1.union (list_2))
Print (list_2 | list_1)
Difference () Method-seeking difference Set
#差集 in list_1 and not in list_2
Print (List_1.difference (list_2))
Print (List_2.difference (list_1))
Determine if it is a subset
List_3 = Set ([1,3,7])
Print (List_3.issubset (list_1))
Print (List_1.issuperset (list_3))
Symmetric_difference () method to find symmetric difference sets
Print (List_1.symmetric_difference (list_2))
Print (list_1 ^ list_2)
Pop () method random Delete
Print (List_1.pop ())
Python notes-----dictionaries, tuple operations