标签:
1、列表List
声明方式:list = [1,2.3,‘x‘,‘Hello‘],拥有方法:
遍历实例:
numbers = [0,1,2,3,4,5,6,7,8,9]
for i in range(len(numbers)):
print(numbers[i])
2、元组Tuple
声明方式比较特殊,tuple = item1,item2,item3,item4。例如:
tuple = 12,323,4,0,57,‘Hello‘
for i in range(len(tuple)):
print(tuple[i])
3、集合Set
声明方式:set = {item1,item2,item3,item4},例如:
basket = {‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘}
集合为无序不重复的元素集,上例声明的结果将为:
{‘e‘, ‘c‘, ‘d‘, ‘b‘, ‘a‘}
遍历方式为:
for i basket:
print(i)
4、字典Dict
声明示例:tel = {‘jack‘:23432,‘scape‘:234}
可使用下述方法进行赋值:tel[‘chunyu‘] = 19910805
结果为:{‘chunyu‘: 19910805, ‘jack‘: 23432, ‘scape‘: 234}
可使用items()方法取得键和对应的值,例如:
for k,v in tel.items():
print(k,v)
遍历方式为:
tel = {‘chunyu‘: 19910805, ‘jack‘: 23432, ‘scape‘: 234}
for key in tel:
print(key, ‘:‘, tel[key])
python的数据结构 -- List, Tuple, Set, Dict
标签:
原文地址:http://www.cnblogs.com/haohonglee/p/4457840.html