赞
踩
- # 直接赋值创建列表
- L1 = [1, 2, 3, 4, 5]
- # list内的数据类型可以不是一个类型
- L2 = [1, 2, 3, "jason"]
-
- print(L1)
- print(L2)
- [1, 2, 3, 4, 5]
- [1, 2, 3, 'jason']
- #
- L3 = list()
- print(L3)
- print(type(L3))
- []
- <class 'list'>
- s = "Jason liu"
-
- L1 = list(s)
- print(type(L1))
- print(L1)
- <class 'list'>
- ['J', 'a', 's', 'o', 'n', ' ', 'l', 'i', 'u']
- # 列表操作案例
-
- L1 = [32, 43, 11, 29, 58]
-
- # 使用下标访问
- print(L1[0])
- #print(L1[15])
-
- # 切片操作需要注意取值范围,左包括右不包括
-
- L1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- print(L1[1:5])
-
- # 切片后生成的是一个全新的列表
- # 通过内置函数id可以判断出切片是否生成了全新的列表
-
- L2 = L1[0:10]
- print(L2)
- print(id(L1))
- print(id(L2))
-
- # 切片下标可以为空
- print(L1[:4])
- print(L1[2:])
- print(L1[:])
-
- # 可以控制增长幅度,默认增长幅度为1
- print(L1[:5:2])
-
- # 下标可以超出范围,超出后不再考虑多余下标内容
- print(L1[:100])
-
- # 下标增长幅度可以为负数
- # 下标为负数,表面顺序是从右往左
- # 规定:数组最后一个元素下标是-1
-
- #下面例子为空,因为默认是从左向右移动
- print(L1[-2:-4])
-
- # 如果想利用负数下标打印9,8
- print(L1[-2:-4:-1])
-
- # 如果想利用负数下标打印8,9
- print(L1[-3:-1])
- 32
- [2, 3, 4, 5]
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- 4370113480
- 4369266760
- [1, 2, 3, 4]
- [3, 4, 5, 6, 7, 8, 9, 10]
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- [1, 3, 5]
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- []
- [9, 8]
- [8, 9]
- list1 = [1, 3, 5, 7, 9, 11, 13, 15, 17]
- list2 = [5, 8, 10, 12, 15, 20, 22, 30]
-
- Set1 = set(list1)
- Set2 = set(list2)
- Set3 = Set1.union(Set2)
- List3 = list(Set3)
- List3.sort(reverse=True)
-
- for i in List3:
- print(i, end=' ')
- 30 22 20 17 15 13 12 11 10 9 8 7 5 3 1
- #append()
-
- # copy() 复制列表
- L1 = [32, 43, 11, 29, 58]
- L2 = L1.copy()
- print(L2)
-
- # count() 计算某个元素在列表中出现的次数
- L1 = [32, 43, 11, 29, 58]
- print(L1.count(32))
-
- # extend() 将一个列表继承另一个列表
- b = [1, 2, 3, 4, 5]
- b.extend([6, 7, 8])
- print(b)
-
- # index() 获取值在列表中的索引
- b = [1, 2, 3, 4, 5, 2, 4]
- print(b.index(2))
- print(b.index(3, 2, 5))
-
- # insert() 在指定位置前插入元素
- b = [1, 2, 3, 4, 5]
- b.insert(2, 9)
- print(b)
-
- # pop() 根据索引移除列表内一个元素,不给索引默认移除最后一个,返回移除的那个值
- b = [1, 2, 3, 4, 5]
- print(b.pop())
- print(b)
-
- # remove() 移除列表中指定的值,返回None
- b = [1, 2, 3, 4, 5]
- b.remove(3)
- print(b)
-
- # reverse() 列表翻转
- b = [1, 2, 3, 4, 5]
- print(id(b))
- b.reverse()
- print(b)
- print(id(b))
-
- #sort() 排序
- b = [4, 2, 5, 8]
- b.sort()
- print(b)
- b.sort(reverse=True)
- print(b)
- [32, 43, 11, 29, 58]
- 1
- [1, 2, 3, 4, 5, 6, 7, 8]
- 1
- 2
- [1, 2, 9, 3, 4, 5]
- 5
- [1, 2, 3, 4]
- [1, 2, 4, 5]
- 4579970184
- [5, 4, 3, 2, 1]
- 4579970184
- [2, 4, 5, 8]
- [8, 5, 4, 2]
- # append()和extend()和insert()
-
- a = [1, 2, 3, 4, 5]
- a.append([6, 7, 8])
- print(a)
-
- b = [1, 2, 3, 4, 5]
- b.extend([6, 7, 8])
- print(b)
-
- c = [1, 2, 3, 4, 5]
- c.insert(3, 10)
- print(c)
- [1, 2, 3, 4, 5, [6, 7, 8]]
- [1, 2, 3, 4, 5, 6, 7, 8]
- [1, 2, 3, 10, 4, 5]
- # 假定给出一个列表 member = ["我", "是", "jason"]
- # 想要把member变成 member = ["我", 99, "是", 88, "jason", 666]
-
- member = ["我", "是", "jason"]
- member.insert(1, 99)
- print(member)
- member.insert(3, 88)
- print(member)
- member.insert(5, 666)
- print(member)
- ['我', 99, '是', 'jason']
- ['我', 99, '是', 88, 'jason']
- ['我', 99, '是', 88, 'jason', 666]
- # 请问如何将下边这个列表的"test"修改为"jason“
- # ls = [1, 2, ['test'], 3, 5, 8, 13, 18]
- ls = [1, 2, ['test'], 3, 5, 8, 13, 18]
- ls[2] = ['jason']
- print(ls)
- [1, 2, ['jason'], 3, 5, 8, 13, 18]
- # 将列表推导式还原出来
- # ls = [(x, y) for x in range(10) for y in range(10) if x%2 == 0 if y%2!=0]
- #ls = [(x, y) for x in range(10) for y in range(10) if x%2 == 0 if y%2!=0]
- #print(ls)
- ls = list()
- for x in range(10):
- for y in range(10):
- if x%2 == 0:
- if y%2 != 0:
- ls.append((x,y))
- print(ls)
- [(0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9), (0, 1), (0, 3), (0, 5), (0, 7), (0, 9), (2, 1), (2, 3), (2, 5), (2, 7), (2, 9), (4, 1), (4, 3), (4, 5), (4, 7), (4, 9), (6, 1), (6, 3), (6, 5), (6, 7), (6, 9), (8, 1), (8, 3), (8, 5), (8, 7), (8, 9)]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。