-1

i have 4 Lists which need to be sorted by Date. One List contains the Filedate,and the others info on Filenamehow without extension ,Size and the last with extension. Now i want to sort the List sorted and the other lists matching the entry Number after sort. I tried with Zip the Listst and then sort, but this didnt work at all.

The Datestring list has Date entries like :

20200329 20200719 20200804 20200919

This is my code how it not works. Am i missing something ?

# ls = []   # loaded filelist
# fs = []   # filename list
# ps = []   # filetype list
# ts = []   # fliedate list

f = open(sys.argv[4], "w")
AB = zip(ts,fs,ps,fz)
AB.sort()
ts[:] = [t[0] for t in AB]
fs[:] = [t[1] for t in AB]
ps[:] = [t[2] for t in AB]
fz[:] = [t[3] for t in AB]
ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • 1
    Can you provide sample data? The `zip()` function returns an iterator, not a list, but you are calling the `sort` method which is not a valid operation on a `zip` object. – ddejohn Oct 29 '21 at 21:37
  • You can create datetime objects from 8 character string. After that python's built-in sort function can sort them. – MSH Oct 29 '21 at 21:38
  • Any particular reason you're using separate lists as opposed to a dictionary or some other data structure which keeps related items grouped together? – ddejohn Oct 29 '21 at 21:38
  • What you're doing should work. Can you post some sample data showing what you got out? – Tim Roberts Oct 29 '21 at 21:39
  • Does this answer your question? [How to sort two lists (which reference each other) in the exact same way](https://stackoverflow.com/questions/9764298/how-to-sort-two-lists-which-reference-each-other-in-the-exact-same-way) – Stef Oct 29 '21 at 21:42

1 Answers1

1

I'll assume your date strings are proper datetime objects, although this would still work with the strings ["20200329", "20200719", "20200804", "20200919"]:

ts, fs, ps, fz = zip(*sorted(zip(ts, fs, ps, fz)))

Demo:

In [4]: ts = ["20200719", "20200804", "20200919", "20200329"]
   ...: fs = ["fs719", "fs804", "fs919", "fs329"]
   ...: ps = ["ps719", "ps804", "ps919", "ps329"]
   ...: fz = ["fz719", "fz804", "fz919", "fz329"]

In [5]: ts, fs, ps, fz = zip(*sorted(zip(ts, fs, ps, fz)))

In [6]: ts
Out[6]: ('20200329', '20200719', '20200804', '20200919')

In [7]: fs
Out[7]: ('fs329', 'fs719', 'fs804', 'fs919')

In [8]: ps
Out[8]: ('ps329', 'ps719', 'ps804', 'ps919')

In [9]: fz
Out[9]: ('fz329', 'fz719', 'fz804', 'fz919')
ddejohn
  • 8,775
  • 3
  • 17
  • 30
  • have to Say that i need this for Python 2.7 ts, fs, ps, fz = [*zip(*sorted(zip(ts, fs, ps, fz)))] ^ SyntaxError: invalid syntax – Mario Keulertz Oct 29 '21 at 21:48
  • Does this not work in 2.7? I don't have an installation of 2.7 with which to test. – ddejohn Oct 29 '21 at 21:49
  • Sad, using the Python 3 solution gives me invalid syntax and the list(... returns invalid syntax for all following code.. File "F:\GP\C64\BBS\Ud-File-reverser-Python\createdirlist.py", line 148 ts, fs, ps, fz = [*zip(*sorted(zip(ts, fs, ps, fz)))] ^ SyntaxError: invalid syntax using.... list(zip(sorted(zip(ts, fs, ps, fz))) File "F:\GP\C64\BBS\Ud-File-reverser-Python\createdirlist.py", line 152 for x in range(len(ls)): ^ SyntaxError: invalid syntax – Mario Keulertz Oct 29 '21 at 21:56
  • See my edit @MarioKeulertz – ddejohn Oct 29 '21 at 21:58
  • Now that doesnt return an error by code Design but "ValueError: too many values to unpack" while running. Each List Contains 1149 Entries. – Mario Keulertz Oct 29 '21 at 22:04
  • As long as you're unpacking the same number of iterables as you pass into the first `zip()`, and assuming none of `ts`, `fs`, `ps`, or `fz` are empty, you should not be getting that error. I've tested in Python 3 and 2.7 and have not run into any issues. Please edit the OP with all of your relevant code. – ddejohn Oct 29 '21 at 22:06