Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help appreciated
#1
Hello! If anyone can help me with this please, I'm new to python and I know this is probably super easy for you guys but I just can't figure it out Wall ...

Sort the list ids by the last four digits of each id. Do this using lambda and not using a defined function. Save this sorted list in the variable sorted_id

ids = [17573005, 17572342, 17579000, 17570002, 17572345, 17579329]
Reply
#2
What have you tried?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
You should start from the beginning. I would start with breaking task into smaller pieces:

- how to get last four digits of an integer
- how to sort (generally) in Python

During this I would keep my eyes open where and how lambda can be used.

You may also think this way: sorting is probably needed quite often and probably there is some built-in method or function for that. I would look for those using Python built-in help.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Any normal sort function would do. The 1757 prefix is common to all, so any sort function will work only on the last 4 digits.
Reply
#5
Look @ this,
ids = [17573005, 17572342, 17579000, 17570002, 17572345, 17579329]
i = []
[i.append(str(id).split('1757')[1]) for id in ids]
j=sorted(i)
sorted_id = ['1757'+id for id in j]
print(sorted_id)
Reply
#6
where´s the lambda function?
Reply
#7
Unfortunately OP has shown no effort. As this is homework I will not provide solutions to this particular problem but to similar problem: 'get integer from the list which has smallest value of last two digits'

There are two tasks:

- how to get last two digits
- how to get minimum value

For first task it's practical to use reminder operator:

>>> 123 % 10
3
>>> 123 % 100
23
For second task there is built-in function min() which accepts key function. How key function works? It takes one argument at the time and outputs one value which is used for determining minimal value. In this particular case lambda takes element from the list and returns its reminder:

>>> nums = [110, 201, 305]
>>> min(nums, key=lambda num: num % 100)
201
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(Aug-22-2019, 06:15 AM)perfringo Wrote:
>>> nums = [110, 201, 305]
>>> min(nums, key=lambda num: num % 100)
201

I'm not understanding this part.. how this min works here? say for iteration 1, it will do like min([110,201,305],key=10).. is my understanding right. Please clarify me and why we need key here? I mess up Sick

Also, how 201 is min when 110 is already there? I feel I'm over looking
Reply
#9
(Aug-22-2019, 08:33 AM)Malt Wrote: I'm not understanding this part.. how this min works here? say for iteration 1, it will do like min([110,201,305],key=10).. is my understanding right. Please clarify me and why we need key here? I mess up Sick

Also, how 201 is min when 110 is already there? I feel I'm over looking

It's always good to start with built-in help:

>>> help(min)
Help on built-in function min in module builtins:

min(...)
    min(iterable, *[, default=obj, key=func]) -> value
    min(arg1, arg2, *args, *[, key=func]) -> value
    
    With a single iterable argument, return its smallest item. The
    default keyword-only argument specifies an object to return if
    the provided iterable is empty.
    With two or more arguments, return the smallest argument.
(END)
Not the easiest read, but one can observe key=func

In simpler terms it can be described as:

Quote:For complex processing, min() can be called with an optional "key=" function that transforms each element before comparison. The key function takes in 1 value and returns 1 value, and the returned "proxy" value is used for finding minimum value.

What happens under the hood? Exactly this: every item in iterable is processed by key function and returned value is used to find minimum value.

It can be illustrated by this example:

>>> nums = [110, 201, 305]
>>> [num % 100 for num in num]       # find reminders
[10, 1, 5]
>>> min(num % 100 for num in nums)   # find smallest reminder
1
Python does it more efficiently but as general idea one can describe principle this way:

>>> nums = [110, 201, 305]
>>> reminders = [num % 100 for num in nums]   
>>> i = reminders.index(min(reminders)) # find index of item with minimum value in reminders
>>> nums[i]                             # find corresponding value in nums
201
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Any help would be appreciated tegtheWegg 1 1,703 Feb-10-2021, 12:52 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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