+ 3
CHALLENGE: Sort a list by it's Float Element in tuple
Program to sort a list by it's Float element(which is string) in tuple. Sample Data: [('ABC','11.75'),('DEF','10.90'),('GHI','8.50')] Output: [('GHI','8.50'),('DEF','10.90'),('ABC','11.75')] I know Python coding, other languages it's open.
13 Respuestas
+ 6
@Ferhat:
It's not easy to be extensive, but some things:
In fact, you continue to think too much complicated...
+ First, in your last code, you define a variable named 'sort' by assigning it a lambda function, so the built-in sort() function is no more accessible (and you not really use it but your lambda function)
+ Next, sort() and sorted() (work on same model, one modify the list, the other return the sorted list) could get named arguments:
> 'key', wich expect a function (named or lambda) wich take one argument the item iterated, and should return the value to be use for the sort (comparison with other items)
> 'reverse', wich default value is False to perform an ascending sort, but could be set to True if you want reverse the sort (descending one)
So, if you have a list of list (or object), you can just do:
def mysort(item):
return item[n] # with n set to the index/key on wich you want to sort the outerlist
data = [[...],[...],...]
sorted_data = data.sorted(key=mysort, reverse=True)
sort(data, key=mysort, reverse=True)
The user sort function can be as complex as wanted, and obviously, you can use lambda function if you can onlining the function (in case of just selecting the key/index on wich operate the sort, that's really less verbose and even readable, as you have seen in my first post here ;)
+ 5
Technically therre are no floats in your tuples, alk strings haha
+ 5
# Python:
data = [('ABC','11.75'),('DEF','10.90'),('GHI','8.50')]
print(sorted(data,key=lambda item: float(item[1])))
# @Luka: that's not Python? in what language is your solution coded?
+ 5
@Ferhat Sevim:
I upvote your post as an encouragement, because I think you're in the good track for learning, but: why do simple when we could do hard? ;)
Check the sort() and sorted() method of list (and my submited solution in onelined Python), if not already done: your code doesn't require all this "collections" stuff... even if by the way, you've learn some interesting things which could be useful in another project ^^
+ 4
@Ferhat Sevim:
Yep!
It's clearer now, isn't it?
But, your code only work for this particular list case, because in reality you are ordering the list by the first ([0] index) item value, reversly ;P
Don't use reverse, and use index [1] instead [0] ;)
@iddu:
While I was writting this answer, I had received a notification of you having marked one of mine answer as best, but it seems that you've retrieve it quickly... is that a mistake?
+ 3
Maybe this isn't exactly what you want.
First one can be what you want.
I have learned new things anyway.
https://code.sololearn.com/c0o4hZ6tlFS4/?ref=app
+ 3
@Visph,
Thanks for your explanation, I solved it now. See below:
https://code.sololearn.com/cB3Uq808UE2J/?ref=app
+ 2
@visph:
Thanks for your response. I have checked sorted that you already posted and sort() I have tried it and didn't get same result.
Surely! :)
+ 1
@visph... cool answer...
+ 1
@luka.. great.. at first i thought it might be c . thanks for update .
+ 1
@Ferhat Sevim.. thanks for the answer..