Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
lists.sort() with cmp
#11
I don't like to do things I don't understand, and lambda is something I don't understand, and until a mid-size wonder occurs that send me a understandable (sic!) explanation of lambda expressions in python, I don't know how this ever could be changed. I can't read those expressions, I can't build them up... so is this possible without lambda, too? cmp at least works with defs, so I have a glimpse what I am doing.
Reply
#12
Lambda is confusing me too. Not so much when I saw it for the first time but yet I try to code what I code and the lambda is the last resort Big Grin
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
I came to this solution, including also a length sorting for the lists among themselfs. (Longer lists shall follow shorter ones.)
Monday = 'Monday'
Tuesday = 'Tuesday'
Wednesday = 'Wednesday'
Thursday = 'Thursday'
Friday = 'Friday'
Saturday = 'Saturday'
Sunday = 'Sunday'

daysdict = {Sunday: 1, Monday: 2, Tuesday: 3, Wednesday: 4,
                Thursday: 5, Friday: 6, Saturday: 7}

list1 = [Wednesday, Tuesday, Saturday, Monday, Sunday]
list2 = [Monday, Tuesday, Friday, Wednesday]
list3 = [Sunday, Saturday, Sunday]
list4 = [Friday, Tuesday, Thursday]

def sortdays(day1, day2):
    return daysdict[day1] - daysdict[day2] 
    
def sortdayslists(list1, list2, ind = 0):
    while len(list1) > ind and len(list2) > ind:
        if daysdict[list1[ind]] < daysdict[list2[ind]]:
            return -1
        elif daysdict[list1[ind]] > daysdict[list2[ind]]:
            return 1
        else:
            ind += 1
            ordnwert = sortdayslists(list1, list2, ind)
            if ordnwert in (-1, 1):
                return ordnwert
    
    if len(list1) < len(list2):
            return 1
    if len(list1) < len(list2):
            return -1
    else:
        return 0

list5 = list2 + [Saturday]
longlist = [list1, list2, list3, list4, list5]
for nlist in longlist:
    nlist.sort(cmp = sortdays)
longlist.sort(cmp = sortdayslists)

print "*-" * 20
for listi in longlist:
    print listi
Maybe there would be faster possibilities, but it seems to work.
Reply
#14
See lambda as just a shorthand to define a one-line anonymous function that returns something. Since the function in anonymous, you cannot reference it by name and have to define it where you use it:
def square(x):
   return x*x

map(square,numbers)
can be written:
map(lambda x:x*x,numbers)
but the anonymousness of the function can be removed by assigning the function to a variable:
Output:
>>>square⁼lambda x:x*x >>>square(3) 9
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#15
Quote:and until a mid-size wonder occurs that send me a understandable (sic!) explanation of lambda expressions in python, I don't know how this ever could be changed.
Some addition to @Ofnuts explanation.
lambda could have had a better name like make_function.
So lambda is making a function with no name(anonymous functions),and def name making a function with name.
>>> def plus_2(arg):
...     return arg + 2
... 
>>> plus_2
<function plus_2 at 0x036CF540>

>>> lambda arg: arg + 2
<function <lambda> at 0x036CF4F8>

>>> type(plus_2)
<class 'function'>
>>> type(lambda arg: arg + 2)
<class 'function'
Using it by given argument:
>>> plus_2(4)
6
>>> (lambda arg: arg + 2)(4)
6
>>> # Or could assigning to a variable name
>>> plus_2 = lambda arg: arg + 2
>>> plus_2(4)
6
Back to sorting:
>>> lst = [('Tom', 'C', 10), ('Kent', 'A', 12), ('Jenny', 'B', 15)]
>>> sorted(lst, key=lambda tup: tup[1])
[('Kent', 'A', 12), ('Jenny', 'B', 15), ('Tom', 'C', 10)]
we could make a named function,there is no magic  Think
>>> def sort_middle(tup):
...     return tup[1]
...     
>>> lst = [('Tom', 'C', 10), ('Kent', 'A', 12), ('Jenny', 'B', 15)]
>>> sorted(lst, key=sort_middle)
[('Kent', 'A', 12), ('Jenny', 'B', 15), ('Tom', 'C', 10)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,279 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 10,088 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,190 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Creating new list from 2 lists after a custom sort pythoneer 12 5,987 Jun-01-2018, 04:55 PM
Last Post: pythoneer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020