Lists and dictionaries are the most common types of data.
List:
Strings are not easy to take out each value:
>>> name= "Xiaobai xiaohong xiaoming" >>> name ' Xiaobai xiaohong xiaoming '
List storage information is large, easy to access, easy to change:
>>> name_list=["Xiaobai", "Xiaohong", "xiaoming"]>>> name_list[' Xiaobai ', ' xiaohong ', ' xiaoming '] >>> name_list[1] ' Xiaohong ' >>> name_list[2] ' xiaoming '
Insert, preceded by insert position, followed by insert value:
>>> Name_list.insert (1, "Xiaolv") >>> name_list[' Xiaobai ', ' xiaolv ', ' xiaohong ', ' xiaoming ']
Insert to end:
>>> name_list.append ("Xiaohai") >>> name_list[' Xiaobai ', ' xiaolv ', ' xiaohong ', ' xiaoming ', ' Xiaohai ‘]
Get multiple values: When you take out the Gubai
>>> name_list[1:3][' xiaolv ', ' Xiaohong ']
Remove the last value:
>>> name_list[-1] ' Xiaohai '
Take out the last few values: also Gubai disregard after
>>> name_list[-3:-1][' Xiaohong ', ' xiaoming ']
Take out the last few or the first few:
>>> name_list[-3:] [' Xiaohong ', ' xiaoming ', ' Xiaohai ']>>> name_list[:3] [' Xiaobai ', ' xiaolv ', ' Xiaohong ']
Delete: List contents can be duplicated, delete only the first one
>>> Name_list.insert (2, ' Xiaobai ') >>> name_list[' Xiaobai ', ' xiaolv ', ' Xiaobai ', ' xiaohong ', ' Xiaoming ', ' Xiaohai ']>>> name_list.remove (' Xiaobai ') #删除时删除第一个 >>> name_list[' xiaolv ', ' Xiaobai ', ' Xiaohong ', ' xiaoming ', ' Xiaohai ']
When you delete all duplicates, you can write a loop until you delete the error, or you can count the number of duplicates, and then delete them multiple times.
>>> name_list[' xiaolv ', ' Xiaobai ', ' xiaohong ', ' xiaoming ', ' Xiaohai ', ' Xiaobai ', ' Xiaobai ', ' Xiaobai ']> >> name_list.count (' Xiaobai ') 4>>> for I in range (Name_list.count (' Xiaobai ')): Name_list.remove (' Xiaobai ') >>> name_list[' xiaolv ', ' xiaohong ', ' xiaoming ', ' Xiaohai ']
Delete the last one:
>>> name_list.pop () ' Xiaohai ' >>> name_list[' xiaolv ', ' xiaohong ', ' xiaoming '
Sort and invert: Lists are ordered and can be sorted
>>age=[2,20,4,6,19,40]>>> age.sort () >>> print age[2, 4, 6, 40]>>> Age.reverse ( ) >>> print age[40, 20, 19, 6, 4, 2]
Two List connections:
>>> Name_list.extend (age) >>> name_list[' Xiaohong ', ' xiaolv ', ' xiaoming ', 40, 20, 19, 6, 4, 2]
Index: The position where the return value is located, and if more than one, returns the first position
>>> name_list.index (' Xiaolv ') 1
Modify:
>>> name_list[1]= "Xiaohai" >>> name_list[' Xiaohong ', ' Xiaohai ', ' xiaoming ', 40, 20, 19, 6, 4, 2]
Dictionary: Dictionary for key-value pairs
>>> data={' name ': ' Xiaobai ', ' age ': ' Job ': ' Engineer '}>>> data{' job ': ' Engineer ', ' Age ': +, ' name ': ' Xiaobai '}>>> print data[' name ']xiaobai>>> print data[' age ']25
Insert:
>>> data[' salary ']=5000>>> data {' job ': ' Engineer ', ' salary ': +, ' age ': +, ' name ': ' XI Aobai '}
Modify:
>>> data[' job ']= ' worker ' >>> data{' job ': ' Worker ', ' salary ': ", ' age ': +, ' name ': ' Xiaobai '}
Delete:
>>> del data[' job ']>>> data{' salary ': +, ' age ': +, ' name ': ' Xiaobai '}
Remove all: Use for loop
>>> data{' salary ': +, ' age ': +, ' name ': ' Xiaobai '} #效率高 >>> for key in Data:print key Salaryagename>>> for Key,val in Data.items ():p rint Key,data[key] #效率低salary 5000age 25name Xiaobai
If you want to remove the value that you don't have, you get an error:
>>> print data[' info ']traceback (most recent call last): File "<stdin>", line 1, in <module>keyerror : ' Info '
The method of not error:
>>> if Data.has_key (' info '):p rint data[' info ') #没有就不会报错 >>> print data.get (' info ') None #没有的话就为None
Delete all:
>>> data{' salary ': +, ' age ': +, ' name ': ' Xiaobai '}>>> data.clear () >>> data{}
Dictionaries and lists can be nested with each other
>>> menu={' Beijing ': {' Chaoyang ': [' Company A ', ' Company B '], ' wangjing ': [' Company C ', ' Company D ']}, ' Shandong ' : {' Jinan ': [' Company E ', ' Company F '], ' Qingdao ': [' Company G ', ' company H ']}}>>> menu{' Beijing ': {' Chaoyang ': [' Company A ', ' Company B '], ' wangjing ': [' Company C ', ' Company D ']}, ' Shandong ': {' Jinan ': [' Company E ', ' Company F '], ' Qing DAO ': [' Company G ', ' company H ']}}>>> print menu[' Beijing ' "{' Chaoyang ': [' Company A ', ' Company B '], ' wangjing ': [' Company C ', ' Company D ']} >>> print menu[' Beijing ' ["Chaoyang"] [' Company A ', ' Company B ']
Python Learning: Lists and dictionaries