python list_2

2022/2/16 17:11:40

本文主要是介绍python list_2,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

# remove(要删除的值)
# list.remove("你xx")

list = ["小明", "小红", "你xx"]
list.insert(2, "小张")
list.append("ajaks")
print(list)
list2 = ["ssnaj", "anskan"]
list.extend(list2)
print(list)
list[1] = "mkans"
print(list)
# del(列表(哪个值))
del(list[4])
print(list)
list.remove("你xx")
print(list)

 

# pop删除末尾数据
list = ["小明", "小红", "你xx"]
list.pop()
print(list)

 

# pop(),删除指定索引的值,功能于del类似

list = ["小明", "小红", "你xx"]
list.pop(0)
print(list)

 

# list.clear()  清空

list = ["小明", "小红", "你xx"]
list.clear()
print(list)

 

# count 统计成员相同数量
# count(值)
# 如果有多个值,返回值的数量,如果没有值则返回0
list = ["刘备", "小红", "刘备", "666", "刘备"]
print(list.count("小红"))
print(list.count("666"))
print(list.count("刘备"))
print(list.count("aaa"))

 

# 返回指定值在列表中的索引编号
# index()
list = ["刘备", "小红", "刘备", "666", "刘备"]
# 从第二值开始统计刘备的数量
print(list.index("刘备", 1))

 

# sort()  升序
# sort(reverse=True)   降序

list = [1,23,123,144,311,1213,]
list.sort()
print(list)

# 降序
list.sort(reverse=True)
print(list)

 

# reverse
# 逆置, 颠倒顺序,反转

list = [1,23,123,144,311,1213,]
list.reverse()
print(list)

 

# 定义一个列表变量,内容如下
# ["张飞","刘备","关羽","刘邦","刘老二","曹操",]
# 把刘老二修改成周瑜

list =  ["张飞","刘备","关羽","刘邦","刘老二","曹操"]
list[4] = "周瑜"
print(list)

 

# 定义一个空列表
# 向列表内添加, 5,9,13这三个数字

list = []
list.append(5)
list.append(9)
list.append(13)
print(list)

 



这篇关于python list_2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程