python的容器分为四种,分别是:列表(list)、集合(set)、元组(tuple)、字典(dict)
list(列表):python的list底层就是基于双向链表结构设计的
遍历列表
index = 0
while index < len(ls2):
print(ls2[index])
index += 1
list的常见方法:
[‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’,
‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]
set(集合):
遍历只能使用for循环遍历
常见方法:
‘add’, ‘clear’, ‘copy’, ‘difference’, ‘difference_update’,
‘discard’, ‘intersection’, ‘intersection_update’,
‘isdisjoint’, ‘issubset’, ‘issuperset’, ‘pop’,
‘remove’, ‘symmetric_difference’,
‘symmetric_difference_update’, ‘union’, ‘update’] tuple(元组):
常见方法:
clear’, ‘copy’, ‘fromkeys’, ‘get’, ‘items’,
‘keys’, ‘pop’, ‘popitem’, ‘setdefault’,
‘update’, ‘values’]
字典(dict):
字典遍历方式:
1、
for key in p:
print(key, p[key])
2、
for key in p.keys():
print(key , p.get(key))
3、
for key, value in p.items():
print(key, value)
for (key, value) in p.items():
print(key, value)
for t in p.items():
print(t[0], t[1])