-3

I'm new to python programming language and recently I've just encountered this problem and I really want to know how to solve it

so here's the input:

list = ["Sam-11,Mark-10,Mike-14","Martha-12,Emma-9,Kyla-13"]

the output should be like this:

Mark-10, Emma-9 

// Prints the names with the least age in the two strings inside the list including their age

Max Ager
  • 1
  • 1
  • 3
    try and find some tutorials on list manipulation. you'll probably find some useful list methods you can use to sort elements. – L_Church Apr 10 '18 at 15:28
  • 2
    make some sort of attempt before asking for an answer – depperm Apr 10 '18 at 15:29
  • Welcome to SO! Please read the [how to ask](https://stackoverflow.com/help/how-to-ask) guide for tips on asking good questions. – wbadart Apr 10 '18 at 15:29
  • 1
    Off the point but I'm really confused about what this list is meant to represent. Would you prefer a dictionary for this instead of a list? A dictionary is basically a key:value pair where a key is linked to a value. So you can have "Mark" linked to the value of 10 – L_Church Apr 10 '18 at 15:31
  • The first string represents boys and their age then the second string represents girls and their age. – Max Ager Apr 10 '18 at 15:33
  • ok. First, you need to put the code you are using. after that, we can help you to figure out a solution. – Kenry Sanchez Apr 10 '18 at 15:34
  • Possible duplicate of [Nested lambda statements when sorting lists](https://stackoverflow.com/questions/49671990/nested-lambda-statements-when-sorting-lists) – pault Apr 10 '18 at 15:35
  • a nested dictionary then could be key:boys with a value of a dictionary with all the boys then another key:girls with a value of a dictionary with all the girls. so many possibilites lol. – L_Church Apr 10 '18 at 15:36

1 Answers1

0

It is a straight forward solution :

print(", ".join( [ min(each_element.split(","),key = lambda x:int(x.split("-")[1])) for each_element in list_obj ] ))

you split each element on the comma and then find max of the resultant list based on the secondary parameter then you join it as you want.

Kenstars
  • 662
  • 4
  • 11
  • 2
    That is not straight forward in the eyes of a new developer... baha – L_Church Apr 10 '18 at 15:33
  • 1
    Also the answer is wrong. This prints `"Mike-14, Emma-9"` not "`Mark-10, Emma-9"`. This is because you are using `max` when you should be using `min` and you are doing a lexicographical sort instead of integer sort. – pault Apr 10 '18 at 15:38
  • Thank you Pault, I didnt notice , that he had asked for a minimum, and yes, thank you for pointing out my mistakes. – Kenstars Apr 10 '18 at 15:43
  • Now it works, but I would not say it's straightforward- clearly you had trouble with it yourself. You could improve this answer with a more detailed explanation for beginners. Also that line is too long, IMO. – pault Apr 10 '18 at 15:44
  • your code works thank you so much ... now I'm trying to figure out the logic behind it for the future sml – Max Ager Apr 10 '18 at 17:27