当列表大小的子列表相似时,从给定列表大小创建分组子列表时出错
Error creating grouped sub-lists from a given list sizes when the sub-lists of the list sizes are similar
我是编程新手,最近才开始学习 Python。非常感谢您就此问题提供的帮助!
我有一个包含两个子列表的列表,每个子列表包含两个项目。我还有另一个名为 Parts 的列表,它规定了这些项目的隔离。虽然下面的代码似乎可以正常工作,但是当 Parts(partition sizes) 列表的子列表是 same/identical 时,不能正常工作。
需要一些专家的帮助或建议。
注意:Parts子列表中int的总和等于对应Items子列表的长度
Screenshot of the Issue
编写代码如下:
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]] #the sublists are identical. Cause of error for this script
List = []
for i in Parts:
Temp = []
A = 0
for j in i:
if j == 0:
x = []
else:
x = Items[Parts.index(i)][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
Print(List)
为了清楚起见,我在下面说明我的输入和输出(当前和预期)。
场景 01:无错误
输入:
项目 = [["A","B"],["C","D"]]
零件数 = [[0,0,2,0],[1,1,0,0]]
输出:
当前输出(符合预期)= [ [ [],[],["A", "B"],[] ] , [ ["C"],["D"],[],[] ] ]
场景 02:错误
输入:
项目 = [["A","B"],["C","D"]]
零件数 = [[1,1,0,0],[1,1,0,0]]
输出:
当前输出= [ [ ["A"],["B"],[],[] ] , [ ["A"],["B"],[],[] ] ]
预期输出 = [ [ ["A"],["B"],[],[] ] , [ ["C"],["D"],[],[] ] ]
我能够从另一个平台得到答案,Dynamo BIM Forum
@AmolShah 建议两种解决方案:
解决方案01: 通过引入另一个变量
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]] #the sublists are identical. Cause of error for this script
List = []
B = 0
for i in Parts:
Temp = []
A = 0
for j in i:
if j == 0:
x = []
else:
x = Items[B][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
B += 1
Print(List)
解决方案 02:修改代码而不添加新变量。
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]] #the sublists are identical. Cause of error for this script
List = []
for i in range(len(Parts)):
Temp = []
A = 0
for j in Parts[i]:
if j == 0:
x = []
else:
x = Items[i][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
Print(List)
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]]
new_list = []
for item,part in zip(Items,Parts):
temp_l =[]
for n,x in enumerate(part):
if x != 0:
temp_l.append(item)
else:
temp_l.append([])
new_list.append(temp_l)
temp_l=[]
print(new_list)
>>> [[['A', 'B'], ['A', 'B'], [], []], [['C', 'D'], ['C', 'D'], [], []]]
我是编程新手,最近才开始学习 Python。非常感谢您就此问题提供的帮助!
我有一个包含两个子列表的列表,每个子列表包含两个项目。我还有另一个名为 Parts 的列表,它规定了这些项目的隔离。虽然下面的代码似乎可以正常工作,但是当 Parts(partition sizes) 列表的子列表是 same/identical 时,不能正常工作。 需要一些专家的帮助或建议。
注意:Parts子列表中int的总和等于对应Items子列表的长度
Screenshot of the Issue
编写代码如下:
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]] #the sublists are identical. Cause of error for this script
List = []
for i in Parts:
Temp = []
A = 0
for j in i:
if j == 0:
x = []
else:
x = Items[Parts.index(i)][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
Print(List)
为了清楚起见,我在下面说明我的输入和输出(当前和预期)。
场景 01:无错误
输入:
项目 = [["A","B"],["C","D"]]
零件数 = [[0,0,2,0],[1,1,0,0]]
输出:
当前输出(符合预期)= [ [ [],[],["A", "B"],[] ] , [ ["C"],["D"],[],[] ] ]
场景 02:错误
输入:
项目 = [["A","B"],["C","D"]]
零件数 = [[1,1,0,0],[1,1,0,0]]
输出:
当前输出= [ [ ["A"],["B"],[],[] ] , [ ["A"],["B"],[],[] ] ]
预期输出 = [ [ ["A"],["B"],[],[] ] , [ ["C"],["D"],[],[] ] ]
我能够从另一个平台得到答案,Dynamo BIM Forum
@AmolShah 建议两种解决方案:
解决方案01: 通过引入另一个变量
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]] #the sublists are identical. Cause of error for this script
List = []
B = 0
for i in Parts:
Temp = []
A = 0
for j in i:
if j == 0:
x = []
else:
x = Items[B][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
B += 1
Print(List)
解决方案 02:修改代码而不添加新变量。
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]] #the sublists are identical. Cause of error for this script
List = []
for i in range(len(Parts)):
Temp = []
A = 0
for j in Parts[i]:
if j == 0:
x = []
else:
x = Items[i][int(A):int(A+j)]
A = j
Temp.append(x)
List.append(Temp)
Print(List)
Items = [["A","B"],["C","D"]]
Parts = [[1,1,0,0],[1,1,0,0]]
new_list = []
for item,part in zip(Items,Parts):
temp_l =[]
for n,x in enumerate(part):
if x != 0:
temp_l.append(item)
else:
temp_l.append([])
new_list.append(temp_l)
temp_l=[]
print(new_list)
>>> [[['A', 'B'], ['A', 'B'], [], []], [['C', 'D'], ['C', 'D'], [], []]]