从两个压缩列表中删除每个子列表中的最后一个项目。

huangapple go评论55阅读模式
英文:

Remove a last item from each sublist nested in two zipped lists

问题

I work with python inside Dynamo and I've a zipped lists (curves, vectors) with nested sublists in both lists, and I want to remove the last item in each sublist

I tried to use zip function in the two level lists, but I get empty lists

curves = [[curve1, curve2, curve3, curve4, curve5], [curve6, curve7, curve8, curve9],[curve10,curve11, curve12], [curve13, curve14]]

vectors= [[vector1, vector2, vector3, vector4, vector5], [vector6, vector7, vector8, vector9],[vector10,vector11, vector12], [vector13, vector14]]

curves1 = []
temp1 = []
vectors1= []
temp2 = []

for i, j in zip(range(len(curves)), range(len(vectors))):
    for c, v in zip(range(len(i)-1), range(len(j)-1)):      
        temp1.append(c)      
        temp2.append(v)  
    curves1.append(temp1)  
    vectors1.append(temp2)  
    temp1 =[]  
    temp2 = []
OUT = curves1 , vectors1

The result should be:

curves1 = [[curve1, curve2, curve3, curve4], [curve6, curve7, curve8],[curve10,curve11], [curve13]]

vectors1= [[vector1, vector2, vector3, vector4], [vector6, vector7, vector8],[vector10,vector11], [vector13]]
英文:

I work with python inside Dynamo and I've a zipped lists (curves, vectors) with nested sublists in both lists, and I want to remove the last item in each sublist

I tried to use zip function in the two level lists, but I get empty lists

curves = [[curve1, curve2, curve3, curve4, curve5], [curve6, curve7, curve8, curve9],[curve10,curve11, curve12], [curve13, curve14]]

vectors= [[vector1, vector2, vector3, vector4, vector5], [vector6, vector7, vector8, vector9],[vector10,vector11, vector12], [vector13, vector14]]

curves1 = []
temp1 = []
vectors1= []
temp2 = []

for i, j in zip(range((0,len(curves)), range(0, len(vectors))):
    for c, v in zip(range(0, len(i)-1), range(0, len(j)-1):      
        temp1.append(c)      
        temp2.append(v)  
    curves1.append(temp1)  
    vectors1.append(temp2)  
    temp1 =[]  
    temp2 = []
OUT = curves1 , vectors1

the result should be:

curves1 = [[curve1, curve2, curve3, curve4], [curve6, curve7, curve8],[curve10,curve11], [curve13]]

vectors1= [[vector1, vector2, vector3, vector4], [vector6, vector7, vector8],[vector10,vector11], [vector13]]

答案1

得分: 1

Here is the translated code:

curves = [['曲线1', '曲线2', '曲线3', '曲线4', '曲线5'], ['曲线6', '曲线7', '曲线8', '曲线9'], ['曲线10', '曲线11', '曲线12'], ['曲线13', '曲线14']]
vectors = [['向量1', '向量2', '向量3', '向量4', '向量5'], ['向量6', '向量7', '向量8', '向量9'], ['向量10', '向量11', '向量12'], ['向量13', '向量14']]

curves1 = []
vectors1 = []

for sublist1, sublist2 in zip(curves, vectors):
    curves1.append(sublist1[:-1])
    vectors1.append(sublist2[:-1])

print(curves1)
print(vectors1)

Please note that I have translated the variable names and list elements into Chinese characters.

英文:
curves = [['curve1', 'curve2', 'curve3', 'curve4', 'curve5'], ['curve6', 'curve7', 'curve8', 'curve9'], ['curve10', 'curve11', 'curve12'], ['curve13', 'curve14']]
vectors = [['vector1', 'vector2', 'vector3', 'vector4', 'vector5'], ['vector6', 'vector7', 'vector8', 'vector9'], ['vector10', 'vector11', 'vector12'], ['vector13', 'vector14']]

curves1 = []
vectors1 = []

for sublist1, sublist2 in zip(curves, vectors):
    curves1.append(sublist1[:-1])
    vectors1.append(sublist2[:-1])


print(curves1)
print(vectors1)

Hope it works

答案2

得分: 1

以下是翻译好的部分:

不需要对这些列表进行压缩。通常情况下,当您希望同时使用多个可迭代对象的项目时,才会使用 zip。这里并非如此。您只是想对多个列表执行相同的操作。

已足够的代码如下:

curves1 = [sublist[:-1] for sublist in curves]
vectors1 = [sublist[:-1] for sublist in vectors]

或者,遵循 DRY 原则,创建一个函数,该函数返回截断子列表后的列表,并将曲线和向量传递给该函数。

def truncate_sublists(inp_list: list):
    return [sublist[:-1] for sublist in inp_list]

curves1 = truncate_sublists(curves)
vectors1 = truncate_sublists(vectors)
英文:

There's no need to zip the lists. Usually you use zip when you want to use the items of multiple iterables simultaneously. That's not the case here. You just want to do the same operation to multiple lists.

This is enough:

curves1 = [sublist[:-1] for sublist in curves]
vectors1 = [sublist[:-1] for sublist in vectors]

Or, in the spirit of DRY, make a function that returns a list with truncated sublists and feed curves and vectors into that.

def truncate_sublists(inp_list: list):
    return [sublist[:-1] for sublist in inp_list]

curves1 = truncate_sublists(curves)
vectors1 = truncate_sublists(vectors)

huangapple
  • 本文由 发表于 2023年4月19日 18:56:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定