Python list 备查

# 原始数据
test = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# 正向切片
print(test[2:4])

[3, 4]

# 反向切片
print(test[-4:-2])

[6, 7]

# 从头切片
print(test[:-2])

[1, 2, 3, 4, 5, 6, 7]

# 从尾切片
print(test[-2:])

[8, 9]

# 分割
print(test[::2])

[1, 3, 5, 7, 9]

# 倒序
print(test[::-1])

[9, 8, 7, 6, 5, 4, 3, 2, 1]

# 分割后倒序
print(test[::-2])

[9, 7, 5, 3, 1]

# set() 去重
a = [1,2,3,4,1,3,5,6,]
b = list(set(a))
print(b)

[1,2,3,4,5,6,]

posted @ 2021-04-25 09:38  太晓  阅读(35)  评论(0编辑  收藏  举报