列表和字典是最常用的数据类型。
列表:
字符串不易取出每个值:
>>> name="xiaobai xiaohong xiaoming" >>> name ‘xiaobai xiaohong xiaoming‘
列表存储信息量大,存取容易,更改方便:
>>> name_list=["xiaobai","xiaohong","xiaoming"] >>> name_list [‘xiaobai‘, ‘xiaohong‘, ‘xiaoming‘] >>> name_list[1] ‘xiaohong‘ >>> name_list[2] ‘xiaoming‘
插入,前面为插入位置,后面为插入值:
>>> name_list.insert(1,"xiaolv") >>> name_list [‘xiaobai‘, ‘xiaolv‘, ‘xiaohong‘, ‘xiaoming‘]
插入到结尾:
>>> name_list.append("xiaohai") >>> name_list [‘xiaobai‘, ‘xiaolv‘, ‘xiaohong‘, ‘xiaoming‘, ‘xiaohai‘]
取得多个值:取出的时候顾前不顾后
>>> name_list[1:3] [‘xiaolv‘, ‘xiaohong‘]
取出最后一个值:
>>> name_list[-1] ‘xiaohai‘
取出最后几个值:也是顾前不顾后
>>> name_list[-3:-1] [‘xiaohong‘, ‘xiaoming‘]
取出最后几个或最前几个:
>>> name_list[-3:] [‘xiaohong‘, ‘xiaoming‘, ‘xiaohai‘] >>> name_list[:3] [‘xiaobai‘, ‘xiaolv‘, ‘xiaohong‘]
删除:列表内容可重复,删除时只删除第一个
>>> name_list.insert(2,‘xiaobai‘) >>> name_list [‘xiaobai‘, ‘xiaolv‘, ‘xiaobai‘, ‘xiaohong‘, ‘xiaoming‘, ‘xiaohai‘] >>> name_list.remove(‘xiaobai‘) #删除时删除第一个 >>> name_list [‘xiaolv‘, ‘xiaobai‘, ‘xiaohong‘, ‘xiaoming‘, ‘xiaohai‘]
删除全部重复的时候,可写一个循环,直到删除到报错为止,或者可统计有多少个重复的,然后删除多次。
>>> 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‘]
删除最后一个:
>>> name_list.pop() ‘xiaohai‘ >>> name_list [‘xiaolv‘, ‘xiaohong‘, ‘xiaoming‘]
排序及反转:列表是有序的,可以排序
>>age=[2,20,4,6,19,40] >>> age.sort() >>> print age [2, 4, 6, 19, 20, 40] >>> age.reverse() >>> print age [40, 20, 19, 6, 4, 2]
两个列表连接:
>>> name_list.extend(age) >>> name_list [‘xiaohong‘, ‘xiaolv‘, ‘xiaoming‘, 40, 20, 19, 6, 4, 2]
索引:返回值所在的位置,若有多个则返回第一个位置
>>> name_list.index(‘xiaolv‘) 1
修改:
>>> name_list[1]="xiaohai" >>> name_list [‘xiaohong‘, ‘xiaohai‘, ‘xiaoming‘, 40, 20, 19, 6, 4, 2]
字典:字典为键值对
>>> data={‘name‘:‘xiaobai‘, ‘age‘:25, ‘job‘:‘engineer‘ } >>> data {‘job‘: ‘engineer‘, ‘age‘: 25, ‘name‘: ‘xiaobai‘} >>> print data[‘name‘] xiaobai >>> print data[‘age‘] 25
插入:
>>> data[‘salary‘]=5000 >>> data {‘job‘: ‘engineer‘, ‘salary‘: 5000, ‘age‘: 25, ‘name‘: ‘xiaobai‘}
修改:
>>> data[‘job‘]=‘worker‘ >>> data {‘job‘: ‘worker‘, ‘salary‘: 5000, ‘age‘: 25, ‘name‘: ‘xiaobai‘}
删除:
>>> del data[‘job‘] >>> data {‘salary‘: 5000, ‘age‘: 25, ‘name‘: ‘xiaobai‘}
全部取出:用for循环
>>> data {‘salary‘: 5000, ‘age‘: 25, ‘name‘: ‘xiaobai‘} #效率高 >>> for key in data:print key salary age name >>> for key,val in data.items():print key,data[key] #效率低 salary 5000 age 25 name xiaobai
如果要取出没有的值,会报错:
>>> print data[‘info‘] Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: ‘info‘
不报错的方法:
>>> if data.has_key(‘info‘):print data[‘info‘] #没有就不会报错 >>> print data.get(‘info‘) None #没有的话就为None
全部删除:
>>> data {‘salary‘: 5000, ‘age‘: 25, ‘name‘: ‘xiaobai‘} >>> data.clear() >>> data {}
字典与列表可以相互嵌套
>>> 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‘], ‘Qingdao‘: [‘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‘]
原文地址:http://11070714.blog.51cto.com/11060714/1757385