Dear, I have several lists that depend on each other, for example in one list I have a code and in another I have the date of that code. My problem is that I had to add a new list of codes that just as has your list of dates. My intention is to create a single list of dates ordered chronologically but keeping the position relative to its list of codes.
What I was doing was pasting the new list of dates and the new list of codes at the end, leaving a single large list of codes and another large list of dates, like this:
for i in range(len(codigo2)): #Union lista principal FDE, Fecha y Status
CODIGOS.append(codigo2[i])
FECHAS.append(fechas2[i])
But by doing this the chronological order is lost since I am only adding the new codes and dates to the end. They know a way to sort a list and keep those new positions for another list.
define a class
Entrada
Then if you have two lists with entries you can mix and order them as follows
If the positions of both lists match, that is, the code in
CODIGOS[x]
corresponds to the date inFECHAS[x]
, what you can do is usezip
:Jorge, the following code explains my comment and gives you a solution to your question. I agree with @FjSevilla, if you are already using
pandas
, why transform the data into lists and then do this ordering?pandas
offers a lot of functionality to operate withdataframes
, you may have to investigate on that side, if not this can surely help youThis is the output:
Take note that each "code" corresponds to the day in the "date". By sorting the "date" we lost the "synchrony", it's like when you sort by one column in Excel and forget to indicate the others. The solution is to maintain a data structure that allows "code" and "date" to be related. One possibility is to use "tuples", with this: you
tuplelist = [e for e in zip(codigo, fechas)]
transform the two lists into another list of tuples, something like[(codigo, fecha), (codigo, fecha),...]
. With this structure you can already do:tuplelist.sort(key=lambda tuplelist: tuplelist[1])
to sort everything by date (column 2). Finally, if you want to leave everything as the original lists, you just have to do an "unzip":As I have told you, it is possible using the Schwartzian transform which, although the idea comes from Perl in Python, can be implemented using
zip
.The idea is simple, it is used
zip
to obtain the pairs of elements and is applied to themsorted
. This gives us a list with the pairs ordered by the first iterable passed tozip
(if this element is equal, the next one is used).This done, we unpack the list returned by
sorted
and pass all its elements as arguments tozip
return to obtain two lists using the elements of the tuples. In Python you can unpack an iterable using*
in front of it.The only complicated thing is to understand the operation of
zip
, I think that with a couple of examples it can be explained better than with words:zip(*c)
It is equivalent to:An example of how to sort using the idea above based on your code:
Departure:
If you notice use
list.extend
to concatenate the lists, consider using this in your code instead of thefor
y -loopappend
you use now.