m = [1,2,3,4]
n = [5,6,7,8]
# 一.增加元素(append,insert,extend)
m.append(5)
print(m)
m.insert(1,3)
print(m)
m.extend(n)
print(m)
# [1, 2, 3, 4, 5]
# [1, 3, 2, 3, 4, 5]
# [1, 3, 2, 3, 4, 5, 5, 6, 7, 8]
# [Finished in 0.1s]
# 二.修改元素(切片索引赋值)
n[1] = '你'
print(n)
n[2:4] = [32,42]
print(n)
# [5, '你', 7, 8]
# [5, '你', 32, 42]
# [Finished in 0.1s]
print(m.index(3,2,9))
print(m.count(3))
# 3
# 2
# [Finished in 0.1s]
# 三.删除
n.remove(32)
print(n)
del n[1]
print(n)
q = n.pop()
print(q)
print(n)
# [5, '你', 42]
# [5, 42]
# 42
# [5]
# [Finished in 0.1s]
# 四.排序,反转
w = [12,3,4,5]
w.sort()
print(w)
w.sort(reverse=True)
print(w)
w.reverse()
print(w)
e = sorted(w,reverse=True)
print(e)
print(w)
r = reversed(w)
print(list(r))
print(list(r))
print(w)
列表方法汇总
最新推荐文章于 2024-07-16 13:58:30 发布