Tu = (111, "xiaoxing", (11,22), [(33,44)],45,)
#1. Writing format
#一般写元组的时候推荐在最后加入逗号,
#元组中的一级元素不可被修改, cannot be added or deleted
Print (TU)
#2. Index values
V1 = tu[0]
#3. Slice values
V2 = Tu[0:3]
Print (v1)
Print (v2)
Three results of the operation:
(111, ' xiaoxing ', (11, 22), [(33, 44)], 45)
111
(111, ' xiaoxing ', (11, 22))
#4. Can be used for loops to iterate over objects
For item in TU:
Print (item)
Operation Result:
111
Xiaoxing
(11, 22)
[(33, 44)]
45
#5. Strings, lists, tuples to convert between each other
s = "XIAOXINGASDF"
Li = ["Asdf", "Qwer"]
Tu = ("Xiao", "Xing")
# (1). String tuples
V1 = tuple (s)
Print (v1)
Operation Result:
(' x ', ' I ', ' a ', ' o ', ' x ', ' I ', ' n ', ' G ', ' a ', ' s ', ' d ', ' F ')
# (2). List of tuples
v2 = tuple (LI)
Print (v2)
Operation Result:
(' asdf ', ' qwer ')
# (3). Tuple Turn List
V3 = List (TU)
Print (v3)
Operation Result:
[' Xiao ', ' Xing ']
Tu = (111, "xiaoxing", (11,22), [(33,44)],45,)
#元组, ordered, tuple-level elements cannot be modified
v = tu[3][0][0]
Print (v)
Running Results 33
Tu[3][0] = 567
#这里能够被修改的原因是tu [3][0], the value taken is [(33,44)] A list, so the list can be modified
Print (TU)
[(33,44)] was replaced [567] in the result of the operation.
(111, ' xiaoxing ', (11, 22), [567], 45)
Tu[3][0][0] = 567
#这里如果这样书写会报错, because tu[3][0][0] takes the element in the (33,44) tuple, because the tuple cannot be modified, so it will error
Tu = (one, "xiaoxing", (11,22), [(22,44)],11,)
#统计在元组中出现的次数
V1 = Tu.count (11)
Print (v1)
Operation Result:
2
V2 = Tu.index (11)
#查看索引值
Print (v2)
Operation Result:
0
############################# #字典 ###########################
#1. Basic structure
#info = {
"K1": "V1", #键值对
"K2": "V2"
}
#2. List, the dictionary cannot be the key of the dictionary, the Vaule in the dictionary can be any value
#布尔值是可以作为key值的, but if there is a duplicate key value, keep only one
info = {
1: ' Asdf ',
"K1": "Asdf",
True: "123",
(11,22): 123,
}
Print (info)
Operation Result:
{1: ' 123 ', ' K1 ': ' asdf ', (11, 22): 123}
#4. Dictionaries are unordered, and you can see the order of the output inconsistent by running the dictionary multiple times
#5. Index values for dictionaries
info = {
"K1": 18,
"K2": True,
"K3": [
11,
[],
(),
22,
33,
{
' KK1 ': ' VV1 ',
' KK2 ': ' VV2 ',
' KK3 ':(11,22),
}
],
' K4 ':(11,22,33,44)
}
v = info["K1"]
Print (v)
Operation Result:
18
V1 = info["K3"][5][' KK3 '][0]
#在这里取的是11
Print (v1)
Operation Result:
11
#6. Deleting elements in a dictionary
info = {
"K1": 18,
"K2": True,
"K3": [
11,
[],
(),
22,
33,
{
' KK1 ': ' VV1 ',
' KK2 ': ' VV2 ',
' KK3 ':(11,22),
}
],
' K4 ':(11,22,33,44)
}
Del info["K1"]
Print (info)
Operation Result:
{' K2 ': True, ' K3 ': [One, [], (), 11, +, {' KK1 ': ' VV1 ', ' KK2 ': ' VV2 ', ' KK3 ': (One)}], ' K4 ': (, 22, 33, 44)}
#删除KK1
Del info["K3"][5]["KK1"]
Print (info)
Operation Result:
{' K1 ': ' K2 ': True, ' K3 ': [One, [], (), +, +, {' KK2 ': ' VV2 ', ' KK3 ': (One)}], ' K4 ': (11, 22, 33, 44)}
#获取key
For item in Info.keys ():
Print (item)
Operation Result:
K1
K2
K3
K4
#7. Get Vaule
For item1 in Info.values ():
Print (ITEM1)
Operation Result:
18
True
[One, [], (), +, +, {' KK1 ': ' VV1 ', ' KK2 ': ' VV2 ', ' KK3 ': (11, 22)}]
(11, 22, 33, 44)
#8. Get both Key and Vaule
For item in Info.keys ():
Print (Item,info[item])
Operation Result:
K1 18
K2 True
K3 [One, [], (), +, +, {' KK1 ': ' VV1 ', ' KK2 ': ' VV2 ', ' KK3 ': (11, 22)}]
K4 (11, 22, 33, 44)
#还有种方法
For k,v in Info.items ():
Print (K,V)
Running results consistent with the above
#布尔值是可以作为key值的, but if there is a duplicate key value, keep only one
info = {
"K1": 18,
"K1": True,
"K3": [
11,
[],
(),
22,
33,
{
' KK1 ': ' VV1 ',
' KK2 ': ' VV2 ',
' KK3 ':(11,22),
}
],
' K4 ':(11,22,33,44)
}
Operation Result:
{' K1 ': True, ' K3 ': [One, [], (), 11, +, {' KK1 ': ' VV1 ', ' KK2 ': ' VV2 ', ' KK3 ': (One)}], ' K4 ': (, 22, 33, 44)}
v = Dict.fromkeys (["K1", 123, "999"],123)
#1. Create a dictionary based on a sequence and specify a uniform value
Print (v)
Operation Result:
{' K1 ': 123, 123:123, ' 999 ': 123}
#2. Gets vaule based on key value, can specify default value if not present, return none when not specified
DIC = {
' K1 ': ' V1 ',
}
v = dic.get (' K1 ', 111)
Print (v)
Operation Result:
V1
DIC = {
' K1 ': ' V1 ',
' K2 ': ' v2 '
}
v = dic.pop (' K1 ')
#3. Delete and get values
V1 = Dic.popitem ()
#随机删除
Print (v1)
Print (DIC,V)
Operation Result:
{' K2 ': ' V2 '} v1
(' K2 ', ' v2 ')
#4. Set the value, if it already exists, do not set, get the value corresponding to the current key
#不存在, set, and get the value corresponding to the current key
V1 = Dic.setdefault (' K1 ', ' 1234 ')
V2 = Dic.setdefault (' kk1 ', "1234")
Print (V1,dic)
Print (V2,dic)
Operation Result:
v1 {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' kk1 ': ' 1234 '}
1234 {' K1 ': ' v1 ', ' K2 ': ' v2 ', ' kk1 ': ' 1234 '}
Two ways to #5. Update calls
Dic.update ({"K1": ' 520 ', ' K3 ': ' 521 '})
Print (DIC)
Operation Result:
{' K1 ': ' 520 ', ' K2 ': ' v2 ', ' K3 ': ' 521 '}
Dic.update (k1=123,k3=234,k4=456)
Print (DIC)
Operation Result:
{' K1 ': 123, ' K2 ': ' v2 ', ' K3 ': 234, ' K4 ': 456}
The dictionary is particularly important in 6. Keys () 7.vaules () 8.items () Get update
See the DAY12 10th
Python Learning "The 4th Chapter": Meta-group Magic