Python学习随笔_2_基础:列表元组
Posted 104cz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习随笔_2_基础:列表元组相关的知识,希望对你有一定的参考价值。
一、列表
列表是Python 最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作。
1、定义列表
1 names = ["Cz","Cao","Zhang","Hui","Yang"] 2 print(names)
2、元素访问:通过下标
1 >>> names = ["Cz","Cao","Zhang","Hui","Yang"] 2 >>> names[0] 3 ‘Cz‘ 4 >>> names[1] 5 ‘Cao‘ 6 >>> names[-1] 7 ‘Yang‘
3、切片访问
1 >>> names = ["Cz","Cao","Zhang","Hui","Yang","Xiang","Xin"] 2 >>> names[1:3] #切片取值为[a,b)区间 3 [‘Cao‘, ‘Zhang‘] 4 >>> names[:3] #默认从头开始取值 5 [‘Cz‘, ‘Cao‘, ‘Zhang‘] 6 >>> names[-3:-1] #切片也能倒取 7 [‘Yang‘, ‘Xiang‘] 8 >>> names[-3:] #若倒取想获得最后一个值,只能用默认值 9 [‘Yang‘, ‘Xiang‘, ‘Xin‘] 10 >>> names[:4:2] #步长为2,取值 11 [‘Cz‘, ‘Zhang‘]
4、追加元素
1 >>> names = ["Cz","Cao","Zhang","Hui","Yang","Xiang","Xin"] 2 >>> names.append(‘New_person‘) 3 >>> print(names) 4 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Xiang‘, ‘Xin‘, ‘New_person‘]
5、插入元素
1 >>> names = ["Cz","Cao","Zhang","Hui","Yang","Xiang","Xin"] 2 >>> names.insert(1,‘New_person‘) 3 >>> names 4 [‘Cz‘, ‘New_person‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Xiang‘, ‘Xin‘]
6、修改元素
1 >>> names 2 [‘Cz‘, ‘New_person‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Xiang‘, ‘Xin‘] 3 >>> names[1] = ‘Old_person‘ 4 >>> names 5 [‘Cz‘, ‘Old_person‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Xiang‘, ‘Xin‘]
7、删除元素
1 >>> names 2 [‘Cz‘, ‘Old_person‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Xiang‘, ‘Xin‘] 3 >>> del names[1] #删除指定下标元素 4 >>> names 5 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Xiang‘, ‘Xin‘] 6 >>> names.remove(‘Xiang‘) #删除指定元素 7 >>> names 8 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Xin‘] 9 >>> names.pop() #删除最后一个元素 10 ‘Xin‘ 11 >>> names 12 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘]
8、获取下标
1 >>> names 2 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘] 3 >>> names.index(‘Zhang‘) 4 2
返回元素第一次出现的下标。
9、统计元素出现次数
1 >>> names 2 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Cz‘] 3 >>> names.count("Cz") 4 2
10、拓展:追加列表
1 num = [1,2,3] 2 >>> names.extend(num) 3 >>> names 4 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Cz‘, 1, 2, 3]
11、列表排序、反转
1 >>> names 2 [‘Cz‘, ‘Cao‘, ‘Zhang‘, ‘Hui‘, ‘Yang‘, ‘Cz‘, 1, 2, 3] 3 >>> names.sort() 4 Traceback (most recent call last): 5 File "<pyshell#42>", line 1, in <module> 6 names.sort() 7 TypeError: unorderable types: int() < str() 8 >>> names[-1] = ‘3‘ 9 >>> names[-2] = ‘2‘ 10 >>> names[-3] = ‘1‘ 11 >>> names.sort() 12 >>> names 13 [‘1‘, ‘2‘, ‘3‘, ‘Cao‘, ‘Cz‘, ‘Cz‘, ‘Hui‘, ‘Yang‘, ‘Zhang‘] 14 >>> #Python 3.x 里不同数据类型不能放在一起排序了 15 >>> names.reverse() #列表反转 16 >>> names 17 [‘Zhang‘, ‘Yang‘, ‘Hui‘, ‘Cz‘, ‘Cz‘, ‘Cao‘, ‘3‘, ‘2‘, ‘1‘]
12、列表copy
1 >>> import copy 2 >>> copy1 = ["aa","bb",["ee","ff"],"cc","dd"] 3 >>> copy2 = copy.copy(copy1) #浅copy 4 >>> copy3 = copy.deepcopy(copy1) #深copy 5 >>> copy1[1] = "bbb" 6 >>> copy1[2][0] = "eee" 7 >>> copy1 8 [‘aa‘, ‘bbb‘, [‘eee‘, ‘ff‘], ‘cc‘, ‘dd‘] 9 >>> copy2 10 [‘aa‘, ‘bb‘, [‘eee‘, ‘ff‘], ‘cc‘, ‘dd‘] 11 >>> copy3 12 [‘aa‘, ‘bb‘, [‘ee‘, ‘ff‘], ‘cc‘, ‘dd‘]
Why?
Becase:学习引用后解释,目前记住浅copy时:copy1修改嵌套列表时,copy2同时被修改。
二、一个好玩但不太实用的列表copy例子
一对夫妻,维护同一个银行账户!虽然实际开发中绝对不会这么做。。。
可能还有其他应用场景,但是我没想到。(就连这个也是别处看到的。。。)
1 >>> import copy 2 >>> person = [‘name‘,[‘saving‘,‘100‘]] 3 >>> p1 = person[:] 4 >>> p2 = person[:] 5 >>> p1 6 [‘name‘, [‘saving‘, ‘100‘]] 7 >>> p2 8 [‘name‘, [‘saving‘, ‘100‘]] 9 >>> p1[0] = ‘cz‘ 10 >>> p2[0] = ‘zhang‘ 11 >>> p1 12 [‘cz‘, [‘saving‘, ‘100‘]] 13 >>> p2 14 [‘zhang‘, [‘saving‘, ‘100‘]] 15 >>> p1[1][1] = 50 #其中一方花了50 16 >>> p1 17 [‘cz‘, [‘saving‘, 50]] 18 >>> p2 19 [‘zhang‘, [‘saving‘, 50]]
三、元组
元组(tuple)跟列表类似,也是存一组数据,只不是它一旦创建,便不能再修改,所以又叫只读列表。
有且仅有的两个方法,如下:
1 >>> names = (‘cz‘,‘zhang‘,‘cao‘) 2 >>> names.count(‘cz‘) 3 1 4 >>> names.index(‘cz‘) 5 0
好,元组学完了。
四、练习
需求(该练习选自http://www.cnblogs.com/alex3714/articles/5717620.html):
- 启动程序后,让用户输入工资,然后打印商品列表
- 允许用户根据商品编号购买商品
- 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
- 可随时退出,退出时,打印已购买商品和余额
1 salary = int(input(‘Please input you salary: ‘)) 2 3 print(‘‘‘This is a list of goods. Please choose! 4 Direct settlement after selection, do not return! 5 You can enter Q to leave! 6 Thank you!‘‘‘) 7 8 commidity = [[1,‘IPhone‘,6188],[2,‘Honer‘,2188],[3,‘Book‘,58],[4,‘Computers‘,7888],[5,‘Water‘,2],[6,‘CPU_I7‘,3288]] 9 shopping_car = [] 10 11 stat = ‘0‘ 12 13 while stat != ‘Q‘: 14 for comm in commidity: 15 print(comm[0],‘ ‘,comm[1],‘ ‘,comm[2]) 16 17 stat = input(‘Please input you selection: ‘) 18 19 if stat == ‘Q‘: 20 break 21 22 if salary >= commidity[int(stat)-1][2]: 23 shopping_car.append(commidity[int(stat)-1]) 24 salary = salary-commidity[int(stat)-1][2] 25 print(‘Choose success!‘) 26 27 if salary < commidity[int(stat)-1][2]: 28 print(‘Sorry, your credit is running low!‘) 29 30 print(‘‘‘Your balance is:{_salary}. 31 You have purchased the following commodities:‘‘‘.format(_salary=salary)) 32 for shop in shopping_car: 33 print(shop[0],‘ ‘,shop[1],‘ ‘,shop[2])
以上是关于Python学习随笔_2_基础:列表元组的主要内容,如果未能解决你的问题,请参考以下文章