黑猴子的家:Python初识数据类型 — 列表

24次阅读

共计 3483 个字符,预计需要花费 9 分钟才能阅读完成。

1、创建列表
code
# Author: 黑猴子

# 方式一
name_list1 = [‘hei hou zi’, ‘seven’, ‘eric’]

# 方式二
name_list2 = list([‘hei hou zi’, ‘seven’, ‘eric’])

names = [“4ZhangYang”, “#!Guyun”,”xXiangPeng”,[“hei hou zi”,”jack”],
“ChenRonghua”,”XuLiangchen”,”ChenRonghua”,”ChenRonghua22″,”ChenRonghua”]
2、打印列表全部内容
code
# Author: 黑猴子

print(names)
输出
[‘4ZhangYang’, ‘#!Guyun’, ‘xXiangPeng’, [‘hei hou zi’, ‘jack’],
‘ChenRonghua’, ‘XuLiangchen’, ‘ChenRonghua’, ‘ChenRonghua22’, ‘ChenRonghua’]
3、取第一个和第三个, 列表索引以 0 开始
code
# Author: 黑猴子

print(names[0],names[2])
输出
4ZhangYang xXiangPeng
4、切片
code
# Author: 黑猴子

# 切片 坐闭右开 从索引 1 开始取,包括索引 1,取到索引 3,不包括索引 3
print(names[1:3])
输出
[‘#!Guyun’, ‘xXiangPeng’]
5、取最后一个,从右边数,第一个
code
# Author: 黑猴子

print(names[-1])
输出
ChenRonghua
6、从右边数,第二个
code
# Author: 黑猴子

print(names[-2])
输出
ChenRonghua22
7、取最后两个
code
# Author: 黑猴子

print(names[-2:])
输出
[‘ChenRonghua22’, ‘ChenRonghua’]
8、切片的时候是从左往右 -> 数的
code
# Author: 黑猴子

print(names[-3:-1])
输出
[‘ChenRonghua’, ‘ChenRonghua22’]
9、从第一个取三个
code
# Author: 黑猴子

print(names[0:3])
输出
[‘4ZhangYang’, ‘#!Guyun’, ‘xXiangPeng’]
10、从第一个取三个,0 可以省略
print(names[:3])
输出
[‘4ZhangYang’, ‘#!Guyun’, ‘xXiangPeng’]
11、追加 放到最后边
names.append(“LeiHaidong”)
print(names)
输出
[‘4ZhangYang’, ‘#!Guyun’, ‘xXiangPeng’, [‘hei hou zi’, ‘jack’],
‘ChenRonghua’, ‘XuLiangchen’, ‘ChenRonghua’,
‘ChenRonghua22’, ‘ChenRonghua’, ‘LeiHaidong’]
12、插入指定位置
names.insert(1,”Chenzhonghua”)
print(names)
输出
[‘4ZhangYang’, ‘Chenzhonghua’, ‘#!Guyun’, ‘xXiangPeng’, [‘hei hou zi’, ‘jack’],
‘ChenRonghua’, ‘XuLiangchen’, ‘ChenRonghua’, ‘ChenRonghua22’,
‘ChenRonghua’, ‘LeiHaidong’]
13、修改
names[2] = “xiedi”
print(names)
输出
[‘4ZhangYang’, ‘Chenzhonghua’, ‘xiedi’, ‘xXiangPeng’, [‘hei hou zi’, ‘jack’],
‘ChenRonghua’, ‘XuLiangchen’, ‘ChenRonghua’, ‘ChenRonghua22’,
‘ChenRonghua’, ‘LeiHaidong’]
14、删除方式一
names.remove(“Chenzhonghua”)
print(names)
输出
[‘4ZhangYang’, ‘xiedi’, ‘xXiangPeng’, [‘hei hou zi’, ‘jack’],
‘ChenRonghua’, ‘XuLiangchen’, ‘ChenRonghua’,
‘ChenRonghua22’, ‘ChenRonghua’, ‘LeiHaidong’]
15、删除方式二
del names[2]
print(names)
输出
[‘4ZhangYang’, ‘xiedi’, [‘hei hou zi’, ‘jack’], ‘ChenRonghua’,
‘XuLiangchen’, ‘ChenRonghua’, ‘ChenRonghua22’,
‘ChenRonghua’, ‘LeiHaidong’]
16、删除方式三,删除最后一个
names.pop()
print(names)
输出
[‘4ZhangYang’, ‘xiedi’, [‘hei hou zi’, ‘jack’], ‘ChenRonghua’,
‘XuLiangchen’, ‘ChenRonghua’, ‘ChenRonghua22’, ‘ChenRonghua’]
17、输入那个下标索引,就删除那个下标索引
names.pop(2)
print(names)
输出
[‘4ZhangYang’, ‘xiedi’, ‘ChenRonghua’, ‘XuLiangchen’,
‘ChenRonghua’, ‘ChenRonghua22’, ‘ChenRonghua’]
18、查找某一个元素的索引下标
print(names.index(“xiedi”))
输出
1
19、根据找到的索引下标,找到元素
print(names[names.index(“xiedi”)])
输出
xiedi
20、查找元素个数
print(names.count(“ChenRonghua”))
输出
3
21、数组反转
names.reverse()
print(names)
输出
[‘ChenRonghua’, ‘ChenRonghua22’, ‘ChenRonghua’, ‘XuLiangchen’,
‘ChenRonghua’, ‘xiedi’, ‘4ZhangYang’]
22、排序,按照 – 阿斯克码 来排序的
names.sort()
print(names)
输出
[‘4ZhangYang’, ‘ChenRonghua’, ‘ChenRonghua’, ‘ChenRonghua’,
‘ChenRonghua22’, ‘XuLiangchen’, ‘xiedi’]
23、清空
#names.clear()
#print(names)
输出
[]
24、多个数组合并
a = [1,2,3]
names.extend(a)
print(names)
输出
[‘4ZhangYang’, ‘ChenRonghua’, ‘ChenRonghua’, ‘ChenRonghua’,
‘ChenRonghua22’, ‘XuLiangchen’, ‘xiedi’, 1, 2, 3]
25、删除变量
#del a
26、复制, 浅复制,只复制第一层
c = [1,2,3,[4,5]]
d = c.copy()
print(c)
print(d)
c[0] = 9
c[3][0] = 8
print(c)
print(d)
输出
[1, 2, 3, [4, 5]]
[1, 2, 3, [4, 5]]
[9, 2, 3, [8, 5]]
[1, 2, 3, [8, 5]]
27、深 copy
import copy
e = copy.deepcopy(c)
c[3][0] = 7
print(c)
print(e)

#for
print(“—–for—–“)
for i in c:
print(i)
输出
[9, 2, 3, [7, 5]]
[9, 2, 3, [8, 5]]
—–for—–
9
2
3
[7, 5]
28、带步长的切片 从零开始到最后,步长为 2
print(names[0:-1:2])
输出
[‘4ZhangYang’, ‘ChenRonghua’, ‘ChenRonghua22’, ‘xiedi’, 2]
29、带步长的切片 从零开始到最后,步长为 2,0 和 -1 可以省略
print(names[::2])
输出
[‘4ZhangYang’, ‘ChenRonghua’, ‘ChenRonghua22’, ‘xiedi’, 2]
30、打印全部 0 和 -1 省略
print(names[:])
输出
[‘4ZhangYang’, ‘ChenRonghua’, ‘ChenRonghua’, ‘ChenRonghua’,
‘ChenRonghua22’, ‘XuLiangchen’, ‘xiedi’, 1, 2, 3]

正文完
 0