len:=length(driverlist);a[j]=a[j]0 00;

Sorting Python list based on the length of the string - Stack Overflow
to customize your list.
Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
J it only takes a minute:
I want to sort a list of strings based on the string length. I tried to use sort as follows, but it doesn't seem to give me correct result.
xs = ['dddd','a','bb','ccc']
xs.sort(lambda x,y: len(x) & len(y))
['dddd', 'a', 'bb', 'ccc']
['dddd', 'a', 'bb', 'ccc']
What might be wrong?
2,49752961
44.7k112366647
When you pass a lambda to sort, you need to return an integer, not a boolean.
So your code should instead read as follows:
xs.sort(lambda x,y: cmp(len(x), len(y)))
is a builtin function such that cmp(x, y) returns -1 if x is less than y, 0 if x is equal to y, and 1 if x is greater than y.
Of course, you can instead use the key parameter:
xs.sort(key = lambda s: len(s))
This tells the sort method to order based on whatever the key function returns.
EDIT: Thanks to balpha and Ruslan below for pointing out that you can just pass len directly as the key parameter to the function, thus eliminating the need for a lambda:
xs.sort(key = len)
And as Ruslan points out below, you can also use the built-in
function rather than the list.sort method, which creates a new list rather than sorting the existing one in-place:
print sorted(xs, key=len)
87.7k45176233
The same as in Eli's answer - just using a shorter form, because you can skip a lambda part here.
Creating new list:
&&& xs = ['dddd','a','bb','ccc']
&&& sorted(xs, key=len)
['a', 'bb', 'ccc', 'dddd']
In-place sorting:
&&& xs.sort(key=len)
['a', 'bb', 'ccc', 'dddd']
I Would like to add how the pythonic key function works while sorting :
Decorate-Sort-Undecorate Design Pattern :
Python’s support for a key function when sorting is implemented using what is known as the
decorate-sort-undecorate design pattern.
It proceeds in 3 steps:
Each element of the list is temporarily replaced with a “decorated” version that includes the result of the key function applied to the element.
The list is sorted based upon the natural order of the keys.
The decorated elements are replaced by the original elements.
Key parameter to specify a function to be called on each list element prior to making comparisons.
5,94231936
Write a function lensort to sort a list of strings based on length.
def lensort(a):
n = len(a)
for i in range(n):
for j in range(i+1,n):
if len(a[i]) & len(a[j]):
temp = a[i]
a[i] = a[j]
a[j] = temp
print lensort(["hello","bye","good"])
45.3k104169
def lensort(list_1):
list_2=[];list_3=[]
for i in list_1:
list_2.append([i,len(i)])
list_2.sort(key = lambda x : x[1])
for i in list_2:
list_3.append(i[0])
return list_3
This works for me!
The easiest way to do this is:
list.sort(key = lambda x:len(x))
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .24719
Stack Overflow works best with JavaScript enabled

我要回帖

更多关于 00后 的文章

 

随机推荐