![](/img/trans.png)
[英]How to iterate nested lists with another list to create a dictionary of lists 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.