Python Forum
Sort a list that is embedded
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort a list that is embedded
#1
Hello, i'm on a personnal project whose objective is to sort a list by name and not by first name.
However i have to keep the couple "name-firstname"

The user has to enter numbers in a list :

 [0, 1]  
These numbers correspond to people who are in this list :

 listepersonnes = [['RIETTE', 'JEAN', 'H', '28'], ['LAPORTE', 'LUCIE', 'H', '38']]
I would like the result to be :
['LAPORTE', 'LUCIE'], ['RIETTE', 'JEAN'] 
I have do it..
listepersonnes = [['RIETTE', 'JEAN', 'H', '28'], ['LAPORTE', 'LUCIE', 'H', '38']]
listetrie = []
for i in range (len(listepersonnes)):
    listetrie.append(listepersonnes[i][0])
    listetrie.sort()
    listetrie.append(listepersonnes[i][1])
 
print(listetrie)
I really don't know how to do it....
I have to do it with function too..

Thanks for help
Reply
#2
I'm not quite clear on what you want the user's input numbers to do.  Could you provide some more detailed input and outputs?

Also:
>>> [person[:2] for person in sorted(listepersonnes)]
[['LAPORTE', 'LUCIE'], ['RIETTE', 'JEAN']]
>>>
Reply
#3
Hello, sorry i'm french ahah :)
Your code is working correctly but he doesn't do the job i need ;)

 listpeople = [['RIETTE', 'JEAN', 'H', '28'], ['LAPORTE', 'LUCIE', 'H', '38']]
 
INPUT :
I ask to enter number or numbers for example it should do the job for: 0, 2
So I want the code to make sort from the 0 to the 2.

PROGRAM :

I have to make a function ( a definiton ) like this
def sortlist(INPUT):
 ????????????
 ????????????
OUTPUT :
It will sort the name of person like what you did past.

 [['LAPORTE', 'LUCIE'], ['RIETTE', 'JEAN']] 

Problem solved ;)with
 "return sorted([i[:2] for i in [maliste[j] for j in selection]])"
Reply
#4
If sorting in-place is fine, then passing the key parameter to .sort is a faster way to do it than sorted().
>>> listpeople = [['RIETTE', 'JEAN', 'H', '28'], ['LAPORTE', 'LUCIE', 'H', '38']]
>>> sort_by = [0, 2]
>>> sort_start, sort_end = sort_by
>>> listpeople.sort(key=lambda person: person[sort_start:sort_end])
>>> listpeople
[['LAPORTE', 'LUCIE', 'H', '38'], ['RIETTE', 'JEAN', 'H', '28']]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 600 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  Sort a list of dictionaries by the only dictionary key Calab 1 500 Oct-27-2023, 03:03 PM
Last Post: buran
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,150 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  [solved] Sort list paul18fr 5 2,895 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Sort List of Lists by Column Nju 1 11,581 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  How to sort os.walk list? Denial 6 11,582 Oct-10-2020, 05:28 AM
Last Post: Denial
  Converting to a list and sort tantony 6 3,244 Oct-07-2019, 03:30 PM
Last Post: perfringo
  List sort - alphanumeric? papsphilip 2 2,471 Oct-05-2019, 09:27 PM
Last Post: papsphilip
  syntax error on list.sort() jjordan33 3 3,221 Jul-10-2019, 04:57 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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