Tuples and Dictionaries

1. Tuple (tuple):

Tuples are very familiar with lists. The main difference is that the elements in them are not allowed to be modified. Tuples are also ordered, with slice operations.

# 1. Index:

>>> t1=('a','b','c')
>>> print(t1[1])
b

# 2. Slicing:

>>> t1=('a','b','c')
>>> print(t1[::-1])
('c', 'b', 'a')

# 3. Note that the so-called unmodifiable tuple is only for the first-level element. If the first-level element is a variable type: that is, a list, a dictionary, or a set, then this variable type can be changed.

>>> t1=('a','b',[1,2,3])
>>> t1[2][0]=2
>>> t1
('a', 'b', [2, 2, 3]) 

 

Second, the dictionary (dictionary)

 

dict.fromkeys()

get()

pop(),popitem

setdefault()

update()

items() values()keys()

 

 

# 1. Dictionary generation: You can assign values ​​directly through key-value pairs. You can also create dictionaries based on sequences and specify uniform values.

>>> v = dict.fromkeys(["k1",123,"999"],123)

>>> v
{'k1': 123, 123: 123, '999': 123}
>>>

# 2. The value can be obtained through the Key. When the Key does not exist, the default value can be specified

>>> dic = {"k1":'v1',"k2":'v2'}

>>> print(dic["k1"])
v1

>>> print(dic.get("k1"))

v1
>>> print(dic.get("k43",100))
100

 # 3, the elements of the dictionary can be obtained by the pop method: 

dic = {
     " k1 " : ' v1 ' ,
     " k2 " : ' v2 '
}
like
v = dic.pop('k1',90)
print(dic,v)     #{'k2': 'v2'} v1

v = dic.pop('k3',90)
print(dic,v)    #{'k1': 'v1', 'k2': 'v2'} 90

k,v = dic.popitem()
print(dic,k,v)    #{'k1': 'v1'} k2 v2

# 4. Set the value, 
it already exists, if not set, get the value corresponding to the current key
Does not exist, set, get the value corresponding to the current key
dic = {
    "k1": 'v1',
    "k2": 'v2'
}
v = dic.setdefault('k1','123')
print(dic,v)  #{'k1': 'v1', 'k2': 'v2'} v1

v=dic.setdefault('k3','v3')
print(dic,v)  #{'k1': 'v1', 'k2': 'v2', 'k3': 'v3'} v3

# 5. Update dictionary, existing modifications, non-existing additions 
dic = {
     " k1 " : ' v1 ' ,
     " k2 " : ' v2 '
}
dic.update({'k1': '111111','k3': 123})
print(dic)               #{'k1': '111111', 'k2': 'v2', 'k3': 123}
dic.update(k1=123,k3=345,k5="asdf") 
print(dic)          #{'k1': 123, 'k2': 'v2', 'k3': 345, 'k5': 'asdf'}

# 6 The values ​​of the dictionary can be any value, but the key of the dictionary cannot be a list, dictionary, set (tuple can)

# 7Dictionaries are unordered and can be indexed by keys: 
dici={ ' k1 ' : ' aaa ' , ' k2 ' : ' bbb ' , ' k3 ' :[1,2,3 ]}

print (say [ ' k1 ' ])
 print (say [ ' k3 ' ] [1 ])

# 8 The dictionary states del to delete: 
dici={ ' k1 ' : ' aaa ' , ' k2 ' : ' bbb ' , ' k3 ' :[1,2,3 ]}

del (dici[ ' k1 ' ])
 print (dici)     # {'k2': 'bbb', 'k3': [1, 2, 3]} 
# 9 The keys, values, items of the dictionary. These three return Not a list, but a traversable type that doesn't support indexing by subscripts (that returned by python2 can). 
dici={ ' k1 ' : ' aaa ' , ' k2 ' : ' bbb ' , ' k3 ' :[1,2,3 ]}
 print (dici.items())      # dict_items([('k1', 'aaa '), ('k2', 'bbb'), ('k3', [1, 2, 3])]) 
print (dici.     dict_keys(['k1', 'k2', 'k3'])
print(dici.values())   #dict_values(['aaa', 'bbb', [1, 2, 3]])
for i in dici.items():  
    print(i)         #('k1', 'aaa')  ('k2', 'bbb') ('k3', [1, 2, 3])
for  in dici.keys():
    print(i)       #k1 k2 k3
for i in dici.values():
    print(i)    #aaa bbb [1,2,3]
View Code

 



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325073238&siteId=291194637