Python入门篇之列表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python入门篇之列表相关的知识,希望对你有一定的参考价值。

1、列表定义

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"] #列表存储,类似数组

2、列表的操作

(1)切片

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"] #列表存储,类似数组
print(names[1],names[3]) #取值,1、3代表下标,从左到右的位置
执行结果:
Guyun Xuliangchen

print(names[1:3]) #切片,顾头不顾尾取[1]、[2]的值
print(names[0:3]) #切片
print(names[:3]) #等同于0:3,从下标0到3
print(names[-1]) #切片,取倒数第一个值
print(names[-2]) #取倒数第二个值
print(names[-2:]) #取最后两个值

(2)增加--append:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
names.append("LeiHaidong") #追加到最后
print(names)
执行结果:
[‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘, ‘LeiHaidong‘]

(3)修改--insert:

names.insert(1,"Chenronghua")  #数据插入,插在哪个位置前面就写这个元素的下标,如插在Guyun前面
names.insert(3,"Xinzhiyu") #插在Guyun后面
print(names)
执行结果:
[‘ZhangYang‘, ‘Chenronghua‘, ‘Guyun‘, ‘Xinzhiyu‘, ‘Xiangpeng‘, ‘Xuliangchen‘, ‘LeiHaidong‘]

(4)查询--index:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
print(names)
print(names.index("Xiangpeng")) #查出Xiangpeng的位置
print(names[names.index("Xiangpeng")]) #获取下标内容
执行结果:
[‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘]
2
Xiangpeng

(5)统计--count:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
print("count-->",names.count("ZhangYang"))
执行结果:
count--> 1

(6)反转--reverse:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
names.reverse()
print(names)
执行结果:
[‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘]
reverse--> [‘Xuliangchen‘, ‘Xiangpeng‘, ‘Guyun‘, ‘ZhangYang‘]

(7)扩展--extend:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
names2 = [1,2,3,4]
print(names)
names.extend(names2)
print("extend-->",names)
执行结果:
[‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘]
extend--> [‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘, 1, 2, 3, 4]

(8)排序--sort:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
names.sort()
print(names)
执行结果:
[‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘, ‘ZhangYang‘]

(9)清除--clear:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
names.clear()
print(names)
执行结果:
[]

(10)复制--copy:

names = ["ZhangYang","Guyun","Xiangpeng","Xuliangchen"]
names2 = names.copy()
print(names)
print(names2)
names[1] = "古云"  #更改Guyun,可以看到names2中并没有修改
print("names-->",names)
print("names2-->",names2)
执行结果:
[‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘]
[‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘]
names--> [‘ZhangYang‘, ‘古云‘, ‘Xiangpeng‘, ‘Xuliangchen‘]
names2--> [‘ZhangYang‘, ‘Guyun‘, ‘Xiangpeng‘, ‘Xuliangchen‘]

(11)列表嵌套复制--copy.deepcopy:

names = ["ZhangYang","Guyun",["alex","jack"],"Xiangpeng","Xuliangchen"] 
names2 = names.copy()
print(names)
print(names2)
names[1] = "古云"
names[2][0] = "ALEX" #更改列表中的alex为ALEX,可以看到names2中也相应更改
print("names-->",names)
print("names2-->",names2)
执行结果:
[‘ZhangYang‘, ‘Guyun‘, [‘alex‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]
[‘ZhangYang‘, ‘Guyun‘, [‘alex‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]
names--> [‘ZhangYang‘, ‘古云‘, [‘ALEX‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]
names2--> [‘ZhangYang‘, ‘Guyun‘, [‘ALEX‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]

★★★★注意:这里的的copy只会拷贝第一层,列表中包含列表,其中列表是指向存放列表的内存地址,所以在更改列表中的元素时,拷贝的部分也会相应地更改。

为了实现深入拷贝,可以导入copy模块:
import copy
names2 = copy.deepcopy(names)
print(names)
print(names2)
names[1] = "古云"
names[2][0] = "ALEX"
print("names-->",names)
print("names2-->",names2)
执行结果:
[‘ZhangYang‘, ‘Guyun‘, [‘alex‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]
[‘ZhangYang‘, ‘Guyun‘, [‘alex‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]
names--> [‘ZhangYang‘, ‘古云‘, [‘ALEX‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]
names2--> [‘ZhangYang‘, ‘Guyun‘, [‘alex‘, ‘jack‘], ‘Xiangpeng‘, ‘Xuliangchen‘]

可以看到拷贝是完全克隆过来的,但是生产过程中一般不会使用,拷贝多一份数据,等于多占据了一份内存空间。

(12)for循环列表:

names = ["ZhangYang","Guyun",["alex","jack"],"Xiangpeng","Xuliangchen"] 
for i in names:
    print(i)
执行结果:
ZhangYang
Guyun
[‘alex‘, ‘jack‘]
Xiangpeng
Xuliangchen

(13)步长切片:

names = ["ZhangYang","Guyun",["alex","jack"],"Xiangpeng","Xuliangchen"] 
print(names[0:-1:2]) #打印第一个到最后一个,步长为2
执行结果:
[‘ZhangYang‘, [‘alex‘, ‘jack‘]]

3、程序练习

程序:购物车程序

需求:

启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额

#!/usr/bin/python
# _*_ coding:utf-8 _*_
# Aothr: Kim

goods_list = [
    [‘Iphone‘,5800],
    [‘Mac pro‘,9800],
    [‘Bike‘,800],
    [‘Watch‘,10600],
    [‘Coffee‘,31],
    [‘Alex Python‘,120]
]
shopping_list = [] #此列表为空当做购物车使用
salary = input("Please input your salary:")
if salary.isdigit():
    salary = int(salary)
    while True:
        for index,item in enumerate(goods_list): #enumerate可以取出列表的下标和内容
            print(index,item)
        user_choice = input("请选择你需要的商品编号:")
        if user_choice.isdigit():
            user_choice = int(user_choice)
            if user_choice < len(goods_list) and user_choice >= 0: #len可以取出列表的长度
                g_item = goods_list[user_choice]
                if g_item[1] <= salary:
                    shopping_list.append(g_item)
                    salary -= g_item[1]
                    print("已成功添加商品%s到购物车,当前余额为\033[31;1m%s\033[0m" % (g_item, salary))
                else:
                    print("您的余额为%s,无法购买此商品,请充值!"%salary)
            else:
                print("您的选择的商品编号%s有误,请重新选择:"%user_choice)
        elif user_choice == ‘q‘:
            print(‘------goods list------‘)
            for p in shopping_list:
                print(p)
            print("您当前余额为%s"%salary)
            exit()
        else:
            print("您的输入有误,请输入正确的商品编号!")

以上是关于Python入门篇之列表的主要内容,如果未能解决你的问题,请参考以下文章

Python入门篇之函数

Python入门篇之装饰器

JIRA python篇之展示多人未完成任务列表

Python入门篇之文件操作和字符编码

Python入门篇之字符串字典集合

python应用篇之Web应用程序——Django入门(中)