python3 将二维列表转换为一维列表

  1. 自带的tkinter模块
from tkinter import _flatten
arr = [[1, 2, 3], [7, 8, 9]]
t = list(_flatten(arr))
print(t)
>>> [1, 2, 3, 7, 8, 9]
  1. 自带的itertools模块
arr = [[1, 2, 3], [7, 8, 9]]
t = list(chain.from_iterable(arr))
print(t)
>>> [1, 2, 3, 7, 8, 9]
  1. 用sum对列表的求和
arr = [[1, 2, 3], [7, 8, 9]]
t = sum(arr, [])
print(t)
>>> [1, 2, 3, 7, 8, 9]

你可能感兴趣的:(python)