1.列表List
可以通过append、input插入数据
可以通过索引访问列表单独项:列表名[下标]
scores = [] scores.append(98) scores.append('123') print(scores) print(scores[1]) ''' [98, '123'] 123 '''2.数组Arrays
from array import array scores = array('d')#声明需要使用的数组类型,d代表浮点型数字 scores.append(97) scores.append(98) print(scores) print(scores[1]) ''' array('d', [97.0, 98.0]) 98.0 '''数组存储的必须为同一类型数据,列表可以存储任何指定类型,会按照单独一项来保存到列表中
在指定位置插入数据:
names = ['Susan','Christopher'] print(len(names)) names.insert(0,'Bill') print(names) names.sort() print(names) ''' 2 ['Bill', 'Susan', 'Christopher'] ['Bill', 'Christopher', 'Susan'] '''3.字典Dictionary
字典和列表都可以存储任何类型,字典可以设置键值对,而不需要记住顺序
#创建一个空字典 ZhangSan = {} ZhangSan['first'] = 'Zhang' ZhangSan['last'] = 'San' LiSi ={'fisrt':'Li','last':'Si'} print(ZhangSan) print(LiSi) ''' 输出 {'first': 'Zhang', 'last': 'San'} {'fisrt': 'Li', 'last': 'Si'} '''在后面添加代码
#创建一个列表 people = [ZhangSan,LiSi] print(people) ''' 输出: {'first': 'Zhang', 'last': 'San'} {'fisrt': 'Li', 'last': 'Si'} [{'first': 'Zhang', 'last': 'San'}, {'fisrt': 'Li', 'last': 'Si'}] '''在列表中添加
#添加 people.append({'first':'Wang','last':'Wu'}) print(people) ''' 输出: [{'first': 'Zhang', 'last': 'San'}, {'fisrt': 'Li', 'last': 'Si'}, {'first': 'Wang', 'last': 'Wu'}] '''
想获得特定项
#特定项 presenters=people[0:2]#0表示从第0项开始,2表示往后取2项,获取不会改变原列表print presenter可以得到
[{'first': 'Zhang', 'last': 'San'}, {'fisrt': 'Li', 'last': 'Si'}]另外 print(len(presenters))可以打印列表长度,这里值为2