# 在 Python 中將列表的列表連線為列表

## 為什麼我們需要將列表的列表連線為列表

1. 它使程式碼清晰易讀，以獲得更好的文件，因為從單一結構比二維結構更容易閱讀。
2. 一維列表在資料科學應用程式中很有幫助，因為你可以快速讀取、過濾或刪除列表中的專案。
3. 當你希望基於集合論過濾資料時，列表也是首選。

## 使用 `for` 迴圈將列表加入列表

``````Listoflists = [[100,90,80],[70,60],[50,40,30,20,10]] #List of Lists

mylist = [] #new list

for a in range(len(Listoflists)): #List of lists level
for b in range (len(Listoflists[a])): #Sublist Level

print("List of Lists:",Listoflists)
print("1-D List:",mylist)
``````

``````List of Lists: [[100, 90, 80], [70, 60], [50, 40, 30, 20, 10]]
1-D List: [100, 90, 80, 70, 60, 50, 40, 30, 20, 10]
``````

## 使用列表推導將列表加入列表

``````listoflists = [["Sahildeep", "Dhruvdeep"], [14.12,8.6,14.01], [100,100]]

#mylist defined using list comprehension
mylist = [item for sublist in listoflists for item in sublist]

print('List of Lists', listoflists)
print('1-D list', mylist)
``````

``````List of Lists [['Sahildeep', 'Dhruvdeep'], [14.12, 8.6, 14.01], [100, 100]]
1-D list ['Sahildeep', 'Dhruvdeep', 14.12, 8.6, 14.01, 100, 100]
``````

## Python 中使用遞迴將列表的列表連線為列表

``````def to1list(listoflists):

if len(listoflists) == 0:
return listoflists

if isinstance(listoflists[0], list):

return listoflists[:1] + to1list(listoflists[1:])

print("MyList:",to1list([["Sahildeep", "Dhruvdeep"], [14.12,8.6,14.01], [100,100]]))
``````

``````MyList: ['Sahildeep', 'Dhruvdeep', 14.12, 8.6, 14.01, 100, 100]
``````

## 使用內建 Python 函式將列表的列表連線為列表

### 使用 `sum()` 函式將列表的列表連線為列表

``````listoflists = [["Sahildeep", "Dhruvdeep"], [14.12,8.6,14.01], [100,100]] #List to be flattened

mylist = sum(listoflists, [])

print("List of Lists:",listoflists)
print("My List:",mylist)
``````

``````List of Lists: [['Sahildeep', 'Dhruvdeep'], [14.12, 8.6, 14.01], [100, 100]]
My List: ['Sahildeep', 'Dhruvdeep', '14.12', '8.6', '14.01', '100', '100']
``````

### 使用 Lambda 函式加入列表列表

``````listoflists = [["Sahildeep", "Dhruvdeep"], [14.12,8.6,14.01], [100,100]] #List to be flattened

to1list = lambda listoflists:[element for item in listoflists for element in to1list(item)] if type(listoflists) is list else [listoflists]

print("List of lists:", listoflists)
print("MyList:", to1list(listoflists))
``````

``````List of lists: [['Sahildeep', 'Dhruvdeep'], [14.12, 8.6, 14.01], [100, 100]]
MyList: ['Sahildeep', 'Dhruvdeep', 14.12, 8.6, 14.01, 100, 100]
``````

## 使用 Functools 將列表的列表連線為列表

``````import operator
import functools

listoflists = [["Sahildeep", "Dhruvdeep"], [14.12,8.6,14.01], [100,100]]
#mylist using functools
mylist = functools.reduce(operator.iconcat, listoflists, [])

print('List of Lists', listoflists)
print('1-D list', mylist)
``````

``````List of Lists [['Sahildeep', 'Dhruvdeep'], [14.12, 8.6, 14.01], [100, 100]]
1-D list ['Sahildeep', 'Dhruvdeep', 14.12, 8.6, 14.01, 100, 100]
``````

## 使用 `Chain()` 方法將列表的列表連線為列表

``````import itertools

listoflists = [["Sahildeep", "Dhruvdeep"], [14.12,8.6,14.01], [100,100]] #List to be flattened

mylist = list(itertools.chain(*listoflists))

print("List of Lists:",listoflists)
print("My List:",mylist)
``````

``````List of Lists: [['Sahildeep', 'Dhruvdeep'], [14.12, 8.6, 14.01], [100, 100]]
My List: ['Sahildeep', 'Dhruvdeep', 14.12, 8.6, 14.01, 100, 100]
``````

## 使用 Numpy 模組將列表的列表連線為列表

Numpy 庫提供了連線子字串並將它們展平為單個一維列表的函式。讓我們看看如何使用 `numpy.concatenate()` 將巢狀列表轉換為列表：

``````import numpy

listoflists = [["Sahildeep", "Dhruvdeep"], [14.12,8.6,14.01], [100,100]] #List to be flattened

mylist = list(numpy.concatenate(listoflists).flat)

print("List of Lists:",listoflists)
print("My List:",mylist)
``````

``````List of Lists: [['Sahildeep', 'Dhruvdeep'], [14.12, 8.6, 14.01], [100, 100]]
My List: ['Sahildeep', 'Dhruvdeep', '14.12', '8.6', '14.01', '100', '100']
``````

## 相關文章 - Python List

• 在 Python 中將字典轉換為列表
• 從 Python 列表中刪除某元素的所有出現
• 在 Python 中從列表中刪除重複項
• 如何在 Python 中獲取一個列表的平均值
• Python 列表方法 append 和 extend 之間有什麼區別
• 如何在 Python 中將列表轉換為字串