一种有序的可变数据类型,用户存储多个元素
my_list = list() # 创建一个空列表
print(my_list) # 输出:[]
my_list = list([1, 2, 3]) # 创建一个包含元素的列表
print(my_list) # 输出:[1, 2, 3]
lst = [] # 定义一个空的列表
lst = [1,2,4,8,9,10] # 在定义列表的时候存入数据
lst = ["张三","李四",'王五'] # 定义列表并赋值
lst = [1,"张三",20,89.5] # 定义列表,存入不同类型的值
# 列表嵌套
lst = [
[1,"张三",20,80],
[2,"李四",21,90],
[3,"王五",18,90]
]
# 通过列表解析的方式赋值
lst = [1,2,3,4,5,6,7,8,9,10]
lst = [x for x in range(10)]
print(lst)
lst = [i*i for i in range(10)]
print(lst)
lst = [i for i in range(0,20,2)]
print(lst)
lst = [i for i in range(0,20) if i%3==0]
print(lst)
a = [i for i in range(1,20,3)]
print(a)
# 根据下标取出值,下标从0开始
print(a[2])
# 根据m:n取出对应的值存入新的列表中,包括m但是不包括n
b = a[2:5]
print(b)
# 0-4的值,存入新的列表
b = a[:5]
print(b)
b = a[2:] # 取2-最后
print(b)
b = a[:] # 全部取出
print(b)
b = a[0:5:2] # 从0开始到4,步长为2
print(b)
b = a[5:0:-1] # 倒着取,但是不取第一个
print(b)
b = a[5::-1] # 倒着取,5之前的数倒着取
print(b)
b = a[::-1] # 倒序取出值
print(b)
lst = [i for i in range(1,20,3)]
print(lst)
for c in lst:
print(c)
lst = ["张三","李四","王五","赵六"]
for c in lst:
print(c)
# 遍历列表,并显示序号
lst = ["张三","李四","王五","赵六","aa"]
for i in range(len(lst)):
print(i,lst[i])
lst = []
lst.append("张三") # 向列表添加值
lst.append("李四") # 向列表添加值
lst.append("王五") # 向列表添加值
lst.append("赵六") # 向列表添加值
print(lst)
lst.insert(1,"新人") # 在下标1的位置插入一个值
print(lst)
lst1 = ["撒是的","塞呀","sd"]
print(lst1)
lst.extend(lst1) # 将列表 lst1 添加到列表lst中
print(lst)
"""拼接两个列表"""
a = [1,2,3,4,5]
b = [1,2,3,4,5]
c = []
for i in range(len(a)):
#d = str(a[i]) + str(b[i])
#c.append(d)
c.append(str(a[i]) + str(b[i]))
print(c)
a = [1,2,3,4,5]
b = a.pop(3) # 删除下标为3的值
print("删除的值为:",b)
print("删除之后的列表是:",a)
a = ["a","b","c","d","e"]
b = a.pop(3) # 删除下标为3的值
print("删除的值为:",b)
print("删除之后的列表是:",a)
a = ["a","b","c","d","e"]
if("f" in a): # 如果 “f" 在 a列表 中存在
a.remove("f") # 删除指定的元素
print(a)
a = ["a","b","c","d","e"]
while True:
print("当前列表内容:",a)
b = input("请输入要删除的元素")
if b in a: # 如果输入的值在列表中存在
a.remove(b)
else:
print("你输入的元素不存在")
"""
在一个循环中删除多个元素,删除列表中所有姓张的值
在一个循环有中对列表做多次删除元素,不能直接在循环中删除,需要顶一个列表将要删除的元素保存起来,然后循环删除
"""
# 直接在遍历中删除多个元素是不对的
# a = ["离散","张三","张思","李四","张三丰"]
# for item in a:
# if item.startswith("张"):
# a.remove(item)
# print(a)
a = ["离散","张三","张思","李四","张三丰"]
b = [] # 用来存放要删除的元素
for item in a:
if item.startswith("张"):
b.append(item) # 将要删除的元素存入b列表中
for item in b:
a.remove(item) # 从a列表中删除对应的元素
print(a)
"""将列表中的姓张的全部改成性李"""
a = ["离散","张三","张思","李四","张三丰"]
for i in range(len(a)):
item = a[i]
if item.startswith("张"):
a[i] = "李"+item[1:]
print(a)
"""
列表排序
sort() # 将列表排序,默认重小到大的排列
sort(reverse=True) # 反向排序
"""
lst = [1,5,3,2,10]
lst.sort() # 将列表排序,默认重小到大的排列
print(lst)
lst.sort(reverse=True) # 反向排序
print(lst)
"""返回指定元素在列表中的位置"""
a = ["离散","张三","张思","李四","张三丰"]
index = a.index("张思") # 返回指定元素在列表中的下标
print(index)
# 为了避免出错,通常加一个判断
if "张思1" in a:
index = a.index("张思1") # 返回指定元素在列表中的下标,如果不存在就报错
print(index)
"""返回指定元素在列表中出现的次数"""
a = [1,2,3,1,1,3,4,5]
# 统计1出现的次数
c = a.count(1) # 统计1出现的次数
print(c)
a =['a','b','a','a','a','c']
c = a.count('a') # 统计a出现的次数
print(c)
"""编写一个程序,将两个列表按照索引位置进行合并"""
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for c in list1:
print(f"{c}{list2[c-1]}")
# 执行结果
1a
2b
3c
"""编写一个程序,将列表中的每个元素都平方"""
list1 = [1, 2, 3]
list2 = []
for c in list1:
list2.append(c * c)
print(list2)
# 执行结果
[1, 4, 9]
"""编写一个程序,去除列表中的每个元素的 所有空格。"""
my_list = [' he llo ', '', 'w or ld', ' s ', 'abcd de ']
list = []
for c in my_list:
list.append(c.replace(' ', ''))
print(list)
# 执行结果
['hello', '', 'world', 's', 'abcdde']
'''求列表中的最大值和最小值'''
my_nested_list = [[3, 1, 4], [1, 5, 9], [2, 6, 5]]
list = []
list += my_nested_list[0]
list += my_nested_list[1]
list += my_nested_list[2]
list.sort()
print(f"最大值是{list[len(list)-1]},最小值是{list[0]}")
'''用一个新的列表,包含原始列表中的所有偶数元素'''
my_nested_list = [[3, 1, 4], [1, 5, 9], [2, 6, 5]]
list = []
for c in my_nested_list:
list += c
list1 = []
for a in list:
if a%2 == 0:
list1.append(a)
print(list1)
"""转置列表"""
my_nested_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# for a in my_nested_list:
# b = my_nested_list.index(a)
# i = 0
# j = my_nested_list[i][b]
# list[b][i] = j
# i += 1
# j = my_nested_list[i][b]
# list[b][i] = j
# i += 1
# j = my_nested_list[i][b]
# list[b][i] = j
# print(list)
for a in my_nested_list:
b = my_nested_list.index(a)
for i in range(0, 3):
j = my_nested_list[i][b]
list[b][i] = j
print(list)
# 执行结果
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]