当前位置:  首页>> 技术小册>> Python合辑4-130个字符串操作示例

在 Python 中将多个变量附加到列表中

  1. volumeA = 100
  2. volumeB = 20
  3. volumeC = 10
  4. vol1 = []
  5. vol2 = []
  6. vol1.extend((volumeA, volumeB, volumeC))
  7. vol2 += [val for name, val in globals().items() if name.startswith('volume')]
  8. print(vol1)
  9. print(vol2)

Output:

  1. [100, 20, 10]
  2. [100, 20, 10]

该分类下的相关小册推荐: