这是清单:

trandom= [(4, 1), (3, 2), (1, 0), (2, 1)]

for indx, tup in enumerate(trandom):
print("original list", trandom)
#remove the tuple that computes the average
removedfromList= trandom.pop(indx)
#updatedList=(trandom, average[indx])
print(trandom+[average[indx]])
updatedList=trandom+[average[indx]]
print(list(chain(*(i if isinstance(i, tuple) else (i,) for i in updatedList))))
break


结果成

 ('original list', [(4, 1), (3, 2), (1, 0), (2, 1)])
[(3, 2), (1, 0), (2, 1), 2.5]


到目前为止,这很好,因为我已经能够将平均值附加为元组的最后一个元素。

我现在的目标是在这种情况下为原始列表(1,0)中的第一个元组分配平均值(0.5),并将该元组展平到列表中,然后计算其平均值和标准差。我必须随机执行此过程。

最佳答案

关于您的要求有很多困惑。

解决语法错误后,代码将输出以下内容:

original list [(4, 1), (3, 2), (1, 0), (2, 1)]
[(3, 2), (1, 0), (2, 1), 0.0]
[3, 2, 1, 0, 2, 1, 0.0]


以下代码为您提供了您描述的输出:

trandom= [(4, 1), (3, 2), (1, 0), (2, 1)]

#remove for loop because you are breaking out of it after 1 iteration
print(("original list", trandom)) #make the output a tuple
updatedList=(trandom+[np.average([i[0] for i in trandom])])
removedfromList= updatedList.pop(0) #remove the first from updated
print (updatedList)
#next line was not in your output
#print(list(it.chain(*(i if isinstance(i, tuple) else (i,) for i in updatedList))))


关于您的第二部分,我不确定您在寻找什么。 (1,0)当然不是原始列表中的第一个元素。请说明您要做什么。

关于python - 删除列表中的元组,然后将平均值分配给已删除的元组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54394453/

10-11 22:51
查看更多