我想找到从
获得的最大长度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找到max,就像我上面的例子一样 (4) (2) 现在我想找到最大总和的长度,在这种情况下为4
答案 0 :(得分:2)
如果您想要列表的最大长度,可以使用literal_eval
的列表理解来获取列表,然后使用max
并以sum
作为获取列表的密钥最多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