Differences in lists, tuples, collections, dictionaries
|
list |
tuple |
| set
dictionary |
English |
List |
Tuple |
Set |
Dict |
can read and write |
Write |
Read-only |
Write |
Write |
Can I repeat |
Is |
Is |
Whether |
Is |
Storage mode |
Value |
Value |
Key (cannot be duplicated) |
Key-value pairs (keys cannot be duplicated) |
is ordered |
Ordered |
Ordered |
Disordered |
Unordered, automatic positive order |
Initialization |
[1,‘a‘] |
(‘a‘, 1) |
set([1,2]) Or{1,2} |
{‘a‘:1,‘b‘:2} |
Add to |
append |
Read-only |
add |
d[‘key‘] = ‘value‘ |
Read elements |
l[2:] |
t[0] |
No |
d[‘a‘] |
Lists, tuples, collections, dictionaries convert list tuples to other
‘‘‘#列表转集合 (out of weight)
List1 = [6,7,8,8,9]
A=set (List1)
Print (a)
‘‘‘
‘‘‘
#两个列表转字典
List1 = [' Key1 ', ' key2 ', ' Key3 ']
List2 = [' 1 ', ' 2 ', ' 3 ']B
=dict (Zip (list1,list2))
Print (b) #嵌套列表转字典
LIST3 = [[' Key1 ', ' value1 '],[' key2 ', ' value2 '],[' Key3 ', ' value3 ']
C=dict (LIST3)Print (c)
‘‘‘
‘‘‘
# list, tuple to string
List2 = [' A ', ' a ', ' B ']
D= '. Join (LIST2)
Print (d)
‘‘‘
‘‘‘T
Up1 = (' A ', ' a ', ' B ')
D= '. Join (TUP1)
Print (d)
‘‘‘
‘‘‘
#字典转其他
# Dictionary converted to string
Dic1 = {' A ': 1, ' B ': 2}
E=str (DIC1)
Print (e)
‘‘‘
‘‘‘
# Dictionary key and value cross-transfer
Dic2 = {' A ': 1, ' B ': 2, ' C ': 3}
Dic3={value:key for key, value in Dic2.items ()}
Print (DIC3)
‘‘‘
#字符串转其他
# string Goto List
s =' AABBCC '
f=List (s)
Print (f)
# String to tuples
g=tuple (s)
Print (g)
# string to go to collection
H=Set (s)
Print (h)
# String to Dictionary
Dic2 = eval ("{' name ': ' Ljq ', ' Age ': 24}")
Print (DIC2)
# Slicing strings
A = ' a b C '
I=a.split (")
print (i)
Python list, tuple, collection, dictionary differences, and mutual conversions