统计某个元素在列表中出现的次数
names = ["曹操","刘备","赵云","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
count = 0
value = "小乔"
for index in range(len(names)):
if names[index] == value:
count += 1
print(count)
在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
names = ["曹操","刘备","赵云","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
value = list(input("请输入一个序列:"))
for index in range(len(value)):
names.append(value[index])
print(names)
# print(names + value)
从列表中找出某个值第一个匹配项的索引位置
names = ["曹操","刘备","赵云","小乔",("貂蝉","吕布"),"诸葛亮","司马懿","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
value = ("貂蝉","吕布")
for index in range(len(names)):
if names[index] == value:
print(index)
break
反向列表中元素
names = ["曹操","刘备","赵云","小乔","貂蝉","吕布","诸葛亮","司马懿","小乔"]
for index in range(len(names)):
for index1 in range(len(names)):
if index > index1:
names[index],names[index1] = names[index1],names[index]
# print(names)
print(names)
网友评论