解压变量

  1. # swap
  2. a, b = b, a
  3. # 支持tuple和list的解压
  4. l = [1, 2]
  5. a, b = l
  6. # 在变量的组合中也可以生效
  7. a, b, c = 1, 3, (4, 5)
  8. # python会自动识别
  9. a, b, (c, d), e = 1, 3, (4, 5), 7

缺省元素

  1. _, _, (c, d), _ = 1, 3, (4, 5), 7
  2. # 取值
  3. a = {}
  4. for _, v in a.items():
  5. print(v)

压缩变量

  1. data = ['wheel', 'factory1', 3, 4, 5, 6, '2020-02-02']
  2. name, factory, *inch, date = data
  3. print(inch)

联合使用

  1. data = ['wheel', 'factory1', 3, 4, 5, 6, '2020-02-02']
  2. name, factory, *_, date = data