Python|扁平化二维列表的方法
给定一个二维列表,编写一个Python程序将给定的列表转换为扁平列表。
方法#1:使用chain.iterable()
Python3
# Python code to demonstrate
# converting 2d list into 1d list
# using chain.from_iterables
# import chain
from itertools import chain
ini_list = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# printing initial list
print ("initial list ", str(ini_list))
# converting 2d list into 1d
# using chain.from_iterables
flatten_list = list(chain.from_iterable(ini_list))
# printing flatten_list
print ("final_result", str(flatten_list))
Python3
# Python code to demonstrate
# converting 2d list into 1d list
# using list comprehension
# import chain
from itertools import chain
ini_list = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# printing initial list
print ("initial list ", str(ini_list))
# converting 2d list into 1d
# using list comprehension
flatten_list = [j for sub in ini_list for j in sub]
# printing flatten_list
print ("final_result", str(flatten_list))
Python3
# Python code to demonstrate
# converting 2d list into 1d list
# using functools.reduce
# import functools
from functools import reduce
ini_list = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# printing initial list
print ("initial list ", str(ini_list))
# converting 2d list into 1d
# using functools.reduce
flatten_list = reduce(lambda z, y :z + y, ini_list)
# printing flatten_list
print ("final_result", str(flatten_list))
Python3
# Python code to demonstrate
# converting 2d list into 1d list
# using sum
ini_list = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# printing initial list
print ("initial list ", str(ini_list))
# converting 2d list into 1d
flatten_list = sum(ini_list, [])
# printing flatten_list
print ("final_result", str(flatten_list))
# This code is contributed by
# Mayank Chaudhary - chaudhary_19
Python3
#Python 3 code to flatten nested list
#contributed by S Lakshman Rao - kaapalx
ini_list=[[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
#Using lambda
flatten_list = lambda y:[x for a in y for x in flatten_list(a)] if type(y) is list else [y]
print("Initial list ",ini_list) #printing initial list
print("Flattened List ",flatten_list(ini_list)) # printing flattened list
Python3
#Python 3 code to flatten nested list
#Contributed by S Lakshman Rao - kaapalx
import numpy
ini_list=[[1, 2, 3],
[3, 6, 7],
[7 ,5, 4]]
print("Initial list ",ini_list) #Printing Initial list
print("Flattened List ",list(numpy.concatenate(ini_list).flat))
#Using numpy to flatten list and printing the result
输出:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
方法 #2:使用列表推导
Python3
# Python code to demonstrate
# converting 2d list into 1d list
# using list comprehension
# import chain
from itertools import chain
ini_list = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# printing initial list
print ("initial list ", str(ini_list))
# converting 2d list into 1d
# using list comprehension
flatten_list = [j for sub in ini_list for j in sub]
# printing flatten_list
print ("final_result", str(flatten_list))
输出:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
方法#3:使用 functools.reduce
Python3
# Python code to demonstrate
# converting 2d list into 1d list
# using functools.reduce
# import functools
from functools import reduce
ini_list = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# printing initial list
print ("initial list ", str(ini_list))
# converting 2d list into 1d
# using functools.reduce
flatten_list = reduce(lambda z, y :z + y, ini_list)
# printing flatten_list
print ("final_result", str(flatten_list))
输出:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
方法 #4:使用 sum
sum 有一个可选参数: sum(iterable [, start])
Python3
# Python code to demonstrate
# converting 2d list into 1d list
# using sum
ini_list = [[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
# printing initial list
print ("initial list ", str(ini_list))
# converting 2d list into 1d
flatten_list = sum(ini_list, [])
# printing flatten_list
print ("final_result", str(flatten_list))
# This code is contributed by
# Mayank Chaudhary - chaudhary_19
输出:
initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
final_result [1, 2, 3, 3, 6, 7, 7, 5, 4]
方法 #5:使用 lambda
Python3
#Python 3 code to flatten nested list
#contributed by S Lakshman Rao - kaapalx
ini_list=[[1, 2, 3],
[3, 6, 7],
[7, 5, 4]]
#Using lambda
flatten_list = lambda y:[x for a in y for x in flatten_list(a)] if type(y) is list else [y]
print("Initial list ",ini_list) #printing initial list
print("Flattened List ",flatten_list(ini_list)) # printing flattened list
输出:
Initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened List [1, 2, 3, 3, 6, 7, 7, 5, 4]
方法 #6:使用 numpy
Python3
#Python 3 code to flatten nested list
#Contributed by S Lakshman Rao - kaapalx
import numpy
ini_list=[[1, 2, 3],
[3, 6, 7],
[7 ,5, 4]]
print("Initial list ",ini_list) #Printing Initial list
print("Flattened List ",list(numpy.concatenate(ini_list).flat))
#Using numpy to flatten list and printing the result
输出:
Initial list [[1, 2, 3], [3, 6, 7], [7, 5, 4]]
Flattened List [1, 2, 3, 3, 6, 7, 7, 5, 4]