我想找到从中获得的最大长度

print (( max(sum(literal_eval(sub)) for sub in row)))

这给了我最大,但是如何在python中获得最大和的长度呢?

我的输入是

[0.0],"[0.0, 0.0]","[1.0, 1.0,1.0]","[1.0, 1.0, 1.0, 1.0]","[0.0, 0.0, 0.0]","[0.0, 0.0]",[0.0]

[0.0],"[0.0, 0.0]","[0.0, 0.0, 0.0]","[1.0, 1.0, 1.0, 0.0]","[0.0, 0.0, 0.0]","[1.0, 1.0]",[1.0]

首先通过max在上面的示例中找到最大值,我得到(4)(2)现在,我想找到max sum的长度,在这种情况下为4

===============>>#1 票数:2

如果您想要列表的最大长度,则可以使用带有literal_eval的列表理解来获取列表的列表,然后使用带有sum max作为其键来获取具有最大sum的列表,然后使用len来获取长度:

>>> s=[0.0],"[0.0, 0.0]","[0.0, 0.0, 0.0]","[1.0, 1.0, 1.0, 1.0]","[0.0, 0.0, 0.0]","[0.0, 0.0]",[0.0]
>>> len(max([ast.literal_eval(i) if isinstance(i,str) else i for i in s],key=sum))
4

  ask by abd translate from so

未解决问题?本站智能推荐: