count():
统计列表中元素重复的次数
name = ["1", "2", "2", "1"] print(name.count("1"))
len():
就算“长度”
如果要统计列表中所有的元素的个数那么用count()函数就不行了,因为count()至少要输入一个参数,此时就可以用到len()函数了,
name = ["1", "2", "2", "1"] print(len(name))
列表的切片:
直接用列表名+[ ](里面的第一个参数表示起始位置,第二个参数是步长,默认是1,可以省略,第三个参数是停止位置,应该是停止位置的前一个,是不包括前一个位置的)
append():
在列表中的最后位置添加元素
name = ["4", "3", "2", "1"] name.append("5") print(name)
name = ["4", "3", "2", "1"] name.reverse() print(name)
insert():
可以在任意位置插入新元素
name = ["4", "3", "2", "1"] name.insert(1,"zhanghuarong") print(name)
替换:
name = ["4", "3", "2", "1"] name[2] = "liming" print(name)
remove():
name = ["4", "3", "2", "1"] name.remove("3") print(name)
reverse():
列表元素翻转
name = ["4", "3", "2", "1"] name.reverse() print(name)
sort():
升序排序
name = ["4", "3", "1", "2"] name.sort() print(name)
extend():
name = ["4", "3", "1", "2"] name2 = [1, 2, 3, 4] name.extend(name2) print(name)
clear():
清除列表所有的元素
name = ["4", "3", "1", "2"] name.clear() print(name)
清除列表元素以后就是一个空列表了。