Python list方法汇总

本篇博客汇总Python中list的常见操作。

切片 Slice

区间遵循左闭右开

  • 负数代表倒数第几项
  • 空代表从头/尾开始
1
2
3
list1 = [2, 3, 4, 5]
print(list1[:-2])
# list1[:-2] = [2, 3]

循环 Loop

1
2
3
4
animals = ['cat', 'dog', 'monkey']
#
for animal in animals:

This is a summary

Any content (support inline tags too).