Meta-group
A python tuple is similar to a list, except that the elements of a tuple cannot be modified .
Tuples use parentheses, and the list uses square brackets.
Tuple creation is simple, just add elements in parentheses and separate them with commas.
tp= (The'a','b')
' Hello World ' # this definition is str type b = ('helloWorld') # when defining tuples, if there is only one element, then the type of B is str C = ('helloworld',)print(type (c))
Tuples have only the count and index methods, as follows:
TP = ('127.0.0.1','3306','b','a','3306', 1, 2, 3, 1, 44)Print(Tp[0])#You can also use the subscript to take a valuePrint(Tp.count ('3306'))#find the number of occurrences of an element in a tuplePrint(Tp.index ('a'))#find the index position of an element
Coercion type conversions for tuples:
' b ' ' C ' = tuple (lis) # Converts the list coercion type to a tupleprint(Type (new_lis))
Functions that can now enforce type conversions are: Int (), str (), float (), list (), tuple ()
Dictionary
A dictionary is another mutable container model and can store any type of object, and the data stored by the dictionary is unordered.
Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is included in curly braces ({}) . The key in the dictionary cannot be defined in duplicate , as shown in the following format:
D = {key1:value1, key2:value2}
Get the method of the dictionary element, as follows:
#dictionary, key cannot be duplicated when definedinfo = {'name':'xiaoming','Sex':'Mans',' Age': 20,'ID': 1}#2 different methods of value: If the passed key does not exist, the [] value will be error;Print(info['name'])#dictionary value, remove the corresponding value by keyPrint(Info.get ('name'))#can also be obtained by the Get method, get (key)Print(Info.get ('Addr','Beijing'))#if the passed key does not exist, the passed-in default value is returned: Beijing; If you do not write the key, you will return none if you cannot get it.
Add a dictionary element by doing the following:
# add element info['addr'Beijing' # by [ Key] = value method can be added element, if key does not exist, then add print(info) info.setdefault ('phone ','13000000000'# can also be passed SetDefault (key,value) Method add element print(info)
Modify the elements in the dictionary by doing the following:
info['ID'= 7 # If key exists, the value corresponding to the key is modified, and if key does not exist, the method adds the element Print(info)
Delete the elements in the dictionary as follows:
# Delete del info['addr' # Delete element info.pop ('addr ') # The dictionary is unordered, passing in the key that needs to be deleted, the Pop method returns the valueprintcorresponding to the deleted key( info)#info.pop (' KK ') #如果删除的key不存在, then error: keyerror: ' KK 'info.clear () # Empty dictionary info.popitem () # random deletion of an element, not very useful
The dictionary loops through the elements as follows:
info = {'name':'xiaoming','Sex':'Mans',' Age': 20,'ID': 1}Print(info.)Keys())#get all keys for the dictionary, execution Result: Dict_keys ([' Name ', ' age ', ' sex ', ' id ')Print(info.)Values())#gets all the value of the dictionary, execution Result: dict_values ([' xiaoming ', ', ' Man ', 1])Print(Info.items ())#printing results for all keys, value, loop traversal when used, execution result: Dict_items ([' Name ', ' xiaoming ') , (' Age ', '), (' Sex ', ' man '), (' "id ', 1)])#Dictionary loop Traversal forKvinch info.items():Print('Key:%s Value:%s'%(k, v)) execution Result: Key:age value:20key:sex value:mankey:name value:xiaomingkey:id value:1
Update Usage for dictionaries:
info = {'a':'xiaoming','Sex':'Mans',' Age': 20,'ID': 1}info2= {'a': 1,'b': 2}info.update (Info2)#Merge two dictionaries into 1, if there is the same key, update value, execute result: {' sex ': ' Man ', ' id ': 1, ' B ': 2, ' age ': $, ' a ': 1}
Print (info)
Exercise: Define a dictionary, modify the values in the dictionary, and modify the color corresponding yellow to green, as follows:
all = { 'Car': { 'Color': ['Red','Yellow','Black'], ' Money': 1111, 'name':'Beijing' }, 'car1': { 'ID': [1, 2, 3], ' Money': 222, 'name':'Shanghai' }, 'car2': { ' City': ['Beijing','Shanghai','Zhengzhou'], ' Money': 3333, 'name':'Henan' }}
The code is as follows:
Car_value = All.get ('Car')#The return value is a dictionary that executes the result: {' name ': ' Beijing ', ' Color ': [' red ', ' yellow ', ' black '], ' money ': 1111}Print(car_value) color_list= Car_value.get ('Color')#The returned result is list, based on key to find the corresponding value, execution result: [' red ', ' yellow ', ' black ']Print(color_list) Color_list_value= Color_list[1]#list based on the subscript value, return the result yellowPrint(color_list_value) color_list[1] ='Green' #change the value of the list, changing the yellow to greenPrint(All.get ('Car'))#execution Result: {' money ': 1111, ' color ': [' red ', ' green ', ' black '], ' name ': ' Beijing '}
Python data types-tuples, dictionaries common operations