Python Basic Data Structure

List 的操作

  • list.append(x)
  • list.extend(iterable)
  • list.insert(i, x)
  • list.remove(x) - remove by value
  • list.pop(i) - remove by index
  • list.pop()
  • list.clear()
  • list.index(x, ?start?, ?end?) - find value
  • list.count(x)
  • list.sort(key=None, reverse=False)
  • list.reverse()
  • list.copy()

Python中的list语法糖(Syntax Sugar)

matrix = [
  [1,  2,  3,  4]
  [5,  6,  7,  8],
  [9, 10, 11, 12],
]

transposed = [[row[i] for row in matrix] for i in range(4)]
# [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]