![]() |
Advanced sorting of a built-in list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Advanced sorting of a built-in list (/thread-7414.html) |
Advanced sorting of a built-in list - Whisper40 - Jan-09-2018 Hello everyone, I'm new to the python forum and I need help about ending my python program. My goal is to sort a built-in list by date of birth of the person from oldest to youngest. Once this sorting is done, I then add the names and first names in relation of course... I tried several things, but unfortunately I only managed to do it by using the person's age ("example: 18 years" but I didn't manage to do it for "18/06/1989"). I would like to do this without using a sort function. So i don't want to reproduce the use of sort function.. The goal is ultimately to make this sorting by calling a function and the return is supposed to be of this type. WILSON MIKE 26/06/1950 EMERIC JAMES 27/06/1960 MOPAL ARTHUR 27/06/1966 At the moment I have been able to do it only with the age of the form "66" and not "26/06/1966" of the person as well as with the use of sorting... listepersonnes = [['WILSON', 'MIKE', 'H', '68'], ['EMERIC', 'JAMES', 'H', '58'], ['MOPAL', 'ARTHUR', 'H', '52']] def agepersonne(): print(sorted([i[:4] for i in listepersonnes], key=lambda x: x[3], reverse = True)) agepersonne()Result is : WILSON MIKE 68 EMERIC JAMES 58 MOPAL ARTHUR 52My new list of persons should be like this : listepersonnes = [['WILSON', 'MIKE', 'H', '26/06/1950'], ['EMERIC', 'JAMES', 'H', '27/06/1960'], ['MOPAL', 'ARTHUR', 'H', '27/06/1966']]Thanks for all ! RE: Advanced sorting of a built-in list - snippsat - Jan-09-2018 You most first check if date string work as date object. >>> from datetime import datetime >>> datetime.strptime('26/06/1950', '%d/%m/%Y') datetime.datetime(1950, 6, 26, 0, 0)Now can sort bye dates. >>> lst = [['WILSON', 'MIKE', 'H', '26/06/1950'], ['EMERIC', 'JAMES', 'H', '27/06/1960'], ['MOPAL', 'ARTHUR', 'H', '27/06/1966']] >>> sorted(lst, key=lambda item: datetime.strptime(item[3], '%d/%m/%Y'), reverse=True) [['MOPAL', 'ARTHUR', 'H', '27/06/1966'], ['EMERIC', 'JAMES', 'H', '27/06/1960'], ['WILSON', 'MIKE', 'H', '26/06/1950']] reverse it will be from oldest to youngest.The first and last name should maybe not be split up,could be one string 'WILSON MIKE' .
RE: Advanced sorting of a built-in list - Whisper40 - Jan-10-2018 Thanks for the answer ! Yes, I understand your grouping between name and first name, but I need to separate them to use them in other functions. But your code use the sort function :) My wish is not to use the sorting function but rather to do it by hand. As you can see I already realized the process with the sorting function but this time I want to do it completely by hand. Thanks for all ! RE: Advanced sorting of a built-in list - Whisper40 - Jan-11-2018 Hello, I slightly modified the list Could you tell me how to sort out the dates ? L = [['WILSON', 'MIKE', 'H', '19500626'], ['EMERIC', 'JAMES', 'H', '19600626'], ['MOPAL', 'ARTHUR', 'H', '19660627']] def triInsertion(L): for i in range(len(L)): if L[i][3] > L[i-1][3]: for k in range(0, i): if L[i] < L[k]: X = L.pop(i) L.insert(k, X) break return L triInsertion(L) RE: Advanced sorting of a built-in list - nilamo - Jan-11-2018 Moved to homework, as this is clearly homework. What do your dates look like? Every post has a different format for them, with the last one not really looking like any sort of date at all. RE: Advanced sorting of a built-in list - buran - Jan-11-2018 (Jan-11-2018, 06:03 PM)nilamo Wrote: the last one not really looking like any sort of date at all.it is YYYYMMDD, or using format string %Y%m%d
RE: Advanced sorting of a built-in list - Whisper40 - Jan-11-2018 (Jan-11-2018, 06:46 PM)buran Wrote:(Jan-11-2018, 06:03 PM)nilamo Wrote: the last one not really looking like any sort of date at all.it is YYYYMMDD, or using format string Yes it's that YYYYMMDD |