簡體   English   中英

如何使用 python 中的另一個列表創建可變大小的列表列表?

[英]How to create variable sized list of lists using another list in python?

我知道標題看起來很奇怪,但我會解釋一下。

假設我將從用戶那里獲得一個包含 n 行和 j 列的列表。 然后,我將在我的數據庫中有 nxj 大小的列表。

我會要求用戶提供另一個列表。 它可以是任何大小,因為我的代碼會將其轉換為 1 行和 k 列大小的列表。

我想通過將第二個列表列號轉換為 j 來將第二個列表 append 轉換為 nxj 列表。

例如,用戶發送的第一個列表如下:

list1 = [[Column1, Column2], [1 , 2], [3 , 4]]

用戶發送了第二個列表,我的代碼將其轉換為 1 xn 大小的列表:

list2 = [5 , 6 , 7 , 8 , 9 , 10]

我想要的是:

list1 = [[Column1, Column2], [1 , 2], [3 , 4], [5 , 6], [7 , 8], [9 , 10]]

我的代碼完美地找到了第一個列表的列號。 問題是將第二個列表轉換為列表的配對列表,其中每個內部列表都是first_list_column_number大小。 由於代碼的可伸縮性, first_list_column_number應該保留在那里。

我嘗試了以下方法,但它不起作用,我無法從來源中找到任何幫助。

for i in range(len(list2)):
   for j in range(first_list_column_number)
       list1.append(list2[j])

但它只會創建相同的整數重復。 它也沒有像我想要的那樣創建列表列表。

任何幫助表示贊賞。

有很多方法可以使用或不使用numpy在任何情況下,這里使用np.array_split() , arrays 的數量等於list2的長度除以list1的最后一個元素的長度。 結果的每個數組都將轉換為列表並用於擴展list

import numpy as np
list1 = [['Column1', 'Column2'], [1 , 2], [3 , 4]]
list2 = [5 , 6 , 7 , 8 , 9 , 10]
list1.extend(map(list,np.array_split(list2,len(list2)/len(list1[-1]))))

print(list1)

Output

[['Column1', 'Column2'], [1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]

如果你想避免任何導入(我討厭它有時也很遲鈍),你可以使用一些簡潔的 python 技巧讓它完全按照你的意願行事。 我建議查看一些 python 庫,其他人在 github 上發布了一些有用的技巧。 我的方法還包括一個功能,如果第二個列表中沒有足夠的項目,它將填充列。 這個 python 代碼應該是您開始的良好基礎:

list1 = [['Column1', 'Column2'], [1 , 2], [3 , 4]]
list2 = [5 , 6 , 7 , 8 , 9 , 10]
list3 = [5 , 6 , 7 , 8 , 9 , 10,11]

goal = [['Column1', 'Column2'], [1 , 2], [3 , 4], [5 , 6], [7 , 8], [9 , 10]]

print(  f'list1 = {list1}', 
        f'list2 = {list2}', 
        f'list3 = {list3}', 
        f'Goal:{goal}'      ,'\n', sep='\n')


def formatList(list1:list, list2:list):
    columnCount = len(list1[0])
    listBuffer = []
    #Groups items up and appends them to the first list
    for i in range(len(list2)):
        listBuffer.append(list2[i])
        if len(listBuffer) == columnCount:
            list1.append(listBuffer)
            listBuffer = []
    #Checks for excess items and padds them to make n colums
    if len(listBuffer) > 0:
        #This produces a list of None repeating x amount of times ie:[None,]*3 == [None,None,None]
        #It then appends that list to the buffer list and then appends the buffer to the first list
        listBuffer += [None,]*(columnCount-len(listBuffer))
        list1.append(listBuffer)
    return list1

print('Result using List1 and list2:',
        f'   {list1}  \n+  {list2}  = ',
        f'=  {formatList(list1.copy(), list2.copy())}', '\n', sep='\n')

print('Result using List1 and list3:',
        f'   {list1}  \n+  {list2}  ',
        f'=  {formatList(list1.copy(), list3.copy())}', '\n', sep='\n')



output="""
list1 = [['Column1', 'Column2'], [1, 2], [3, 4]]
list2 = [5, 6, 7, 8, 9, 10]
list3 = [5, 6, 7, 8, 9, 10, 11]
Goal:[['Column1', 'Column2'], [1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]


Result using List1 and list2:
   [['Column1', 'Column2'], [1, 2], [3, 4]]  
+  [5, 6, 7, 8, 9, 10]  = 
=  [['Column1', 'Column2'], [1, 2], [3, 4], [5, 6], [7, 8], [9, 10]]


Result using List1 and list3:
   [['Column1', 'Column2'], [1, 2], [3, 4]]  
+  [5, 6, 7, 8, 9, 10]  
=  [['Column1', 'Column2'], [1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, None]]
"""

我已經在我的 vscode 解釋器中使用 python 3.7.6 運行它,所以它應該是功能齊全的並產生我給出的 output。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM