如何找到最长的列表元素列表 [英] how to find the longst element list of lists
问题描述
如何找到最长的列表元素列表?
我想,应该有一个更简单的方法:
s1 = [" q"," e"," d"]
s2 = [" a"," b"]
s3 = [" ; a"," b"," c"," d"]
如果len(s1)> = len(s2)和len(s1)> = len(s3):
sx1 = s1 ## s1 ist l?ngster
如果len(s2)> = len(s3):
sx2 = s2
sx3 = s3
else:
sx2 = s3
sx3 = s2 <如果len(s2)> = len(s3)和len(s2)> = len(s1):
sx1 = s2 ## s2,则
ist l?ngster
如果len(s3)> = len(s1)
:
sx2 = s3
sx3 = s1 >
else:
sx2 = s1
sx3 = s3
如果len(s3)> = len (s1)和len(s3)> = len(s2):
sx1 = s3 ## s3 ist l?ngster
if len(s1)> = len(s2):
sx2 = s1
sx3 = s2
else:
sx2 = s2
sx3 = s1
之后,列表排序:
sx1 = [" a"," b" ,c,d,
sx2 = [" q"," e"," d"]
sx3 = [" a"," b"]
How to find the longst element list of lists?
I think, there should be an easier way then this:
s1 = ["q", "e", "d"]
s2 = ["a", "b"]
s3 = ["a", "b", "c", "d"]
if len(s1) >= len(s2) and len(s1) >= len(s3):
sx1=s1 ## s1 ist l?ngster
if len(s2) >= len(s3):
sx2=s2
sx3=s3
else:
sx2=s3
sx3=s2
if len(s2) >= len(s3) and len(s2) >= len(s1):
sx1=s2 ## s2 ist l?ngster
if len(s3) >= len(s1):
sx2=s3
sx3=s1
else:
sx2=s1
sx3=s3
if len(s3) >= len(s1) and len(s3) >= len(s2):
sx1=s3 ## s3 ist l?ngster
if len(s1) >= len(s2):
sx2=s1
sx3=s2
else:
sx2=s2
sx3=s1
After, the list ist sorted:
sx1 = ["a", "b", "c", "d"]
sx2 = ["q", "e", "d"]
sx3 = ["a", "b"]
推荐答案
Michael M. kirjoitti:
Michael M. kirjoitti:
如何找到列表中最长的元素列表?
我认为应该有一个更简单的方法:
s1 = [" q,e,d]
s2 = [" a"," b"]
s3 = [ a,b,c,d,
< snip>
之后,列表排序:
sx1 = [" a"," b"," c"," d"]
sx2 = [" q"," e"," d"]
sx3 = [" a"," b"]
How to find the longst element list of lists?
I think, there should be an easier way then this:
s1 = ["q", "e", "d"]
s2 = ["a", "b"]
s3 = ["a", "b", "c", "d"]
<snip>
After, the list ist sorted:
sx1 = ["a", "b", "c", "d"]
sx2 = ["q", "e", "d"]
sx3 = ["a", "b"]
s1 = [" q"," e"," d"]
s2 = [" a"," b"]
s3 = [" a,b,c,d]
ss =((len(s1),s1),(len(s2), s2),(len(s3),s3))
sx = [y for(x,y)in sorted(ss)[:: - 1]]
打印sx
sx1,sx2,sx3 = sx
打印sx1,sx2,sx3
干杯,
Jussi
s1 = ["q", "e", "d"]
s2 = ["a", "b"]
s3 = ["a", "b", "c", "d"]
ss = ((len(s1), s1), (len(s2), s2), (len(s3), s3))
sx = [y for (x, y) in sorted(ss)[::-1]]
print sx
sx1, sx2, sx3 = sx
print sx1, sx2, sx3
Cheers,
Jussi
Michael M.aécrit:
Michael M. a écrit :
如何找到列表中最长的元素列表?
How to find the longst element list of lists?
对于find的定义?你想要最长的
子列表的长度,它的索引,还是对它的引用?
For what definition of "find" ? You want the lenght of the longest
sublist, it''s index, or a reference to it ?
我想,应该这是一个更简单的方法:
s1 = [" q"," e"," d"]
s2 = [" a"," b"]
s3 = [" a"," b"," c"," d"]
I think, there should be an easier way then this:
s1 = ["q", "e", "d"]
s2 = ["a", "b"]
s3 = ["a", "b", "c", "d"]
错误...这使得三个不同的列表,而不是列表列表。
Err... this makes three distinct lists, not a list of lists.
如果len(s1)> = len(s2)和len(s1)> = len(s3):
sx1 = s1 ## s1 ist l?ngster
如果len(s2)> = len(s3) :
sx2 = s2
sx3 = s3
否则:
sx2 = s3
sx3 = s2
if len(s1) >= len(s2) and len(s1) >= len(s3):
sx1=s1 ## s1 ist l?ngster
if len(s2) >= len(s3):
sx2=s2
sx3=s3
else:
sx2=s3
sx3=s2
(剪掉重复的代码)
看起来是时候学习如何分解了重复...
(snip repeated code)
Looks like it would be time to learn how to factor out repetitions...
之后,列表是排序d:
sx1 = [" a,b,c,d]
sx2 = [" q"," e"," d"]
sx3 = [" a"," b"]
After, the list ist sorted:
sx1 = ["a", "b", "c", "d"]
sx2 = ["q", "e", "d"]
sx3 = ["a", "b"]
这仍然不是列表清单。现在得到答案,sorted()是你的朋友:
打印排序([s1,s2,s3],key = list .__ len __,reverse =真)
= [[''a'',''b'',''c'',''d'',[''q'',''e' ',''''',['''',''b'']]
#或者如果你真的想要sx1,sx2和sx3:
sx1,sx2,sx3 =已排序([s1,s2,s3],key = list .__ len __,reverse = True)
这样更容易吗? - )
This is still not a list of lists. Now for the answer, sorted() is your
friend:
print sorted([s1, s2, s3], key=list.__len__, reverse=True)
=[[''a'', ''b'', ''c'', ''d''], [''q'', ''e'', ''d''], [''a'', ''b'']]
# Or if you really want sx1, sx2 and sx3:
sx1, sx2, sx3 = sorted([s1, s2, s3], key=list.__len__, reverse=True)
Is that easier enough ?-)
2007年1月7日,Michael M.< mi ***** @ mustun.chwrote:
On 1/7/07, Michael M. <mi*****@mustun.chwrote:
如何找到最长的列表元素列表?
How to find the longst element list of lists?
s1 = [" q"," e"," d"]
s2 = [" a", b,
s3 = [" a,b,c,d]
s = [ s1,s2,s3]
s.sort(key = len,reverse = True)
打印s [0]是s3
打印s [1]是s1
打印s [2]是s2
sx1,sx2,sx3 = s
print' 'sx1:'',sx1
打印''sx2:'',sx2
打印'sx3:'',sx3
-
Felipe。
s1 = ["q", "e", "d"]
s2 = ["a", "b"]
s3 = ["a", "b", "c", "d"]
s = [s1, s2, s3]
s.sort(key=len, reverse=True)
print s[0] is s3
print s[1] is s1
print s[2] is s2
sx1, sx2, sx3 = s
print ''sx1:'', sx1
print ''sx2:'', sx2
print ''sx3:'', sx3
--
Felipe.
这篇关于如何找到最长的列表元素列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!