+ 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.

30th Oct 2017, 10:47 AM
Siddu Kori
Siddu Kori - avatar
13 Antworten
+ 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 ;)
30th Oct 2017, 5:12 PM
visph
visph - avatar
+ 5
Technically therre are no floats in your tuples, alk strings haha
30th Oct 2017, 11:13 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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?
30th Oct 2017, 11:24 AM
visph
visph - avatar
+ 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 ^^
30th Oct 2017, 4:26 PM
visph
visph - avatar
+ 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?
30th Oct 2017, 6:39 PM
visph
visph - avatar
+ 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
30th Oct 2017, 3:56 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 3
@Visph, Thanks for your explanation, I solved it now. See below: https://code.sololearn.com/cB3Uq808UE2J/?ref=app
30th Oct 2017, 6:27 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 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! :)
30th Oct 2017, 4:51 PM
Ferhat Sevim
Ferhat Sevim - avatar
+ 1
@visph... cool answer...
30th Oct 2017, 11:29 AM
Siddu Kori
Siddu Kori - avatar
+ 1
@luka.. great.. at first i thought it might be c . thanks for update .
30th Oct 2017, 11:31 AM
Siddu Kori
Siddu Kori - avatar
+ 1
@Ferhat Sevim.. thanks for the answer..
30th Oct 2017, 6:34 PM
Siddu Kori
Siddu Kori - avatar