2

Okay, I'm quite new to python and I hope you can help me with this problem I have.

I'm having two lists, who correspond to each other. Now I want to sort them both in the same way.

I found a solution to that here on the board: Is it possible to sort two lists(which reference each other) in the exact same way?

My problem is now, that one of my lists are strings with dates. I want to sort chronological, but i can't figure ou how to use a key parameter proberly. Here is the code I have:

daten = ["03. Aug 2012", "30. Jul 2012", "25. Jul 2012"]
werte = [10, 35, 22]
daten, werte = (list(t) for t in zip(*sorted(zip(daten, werte), key=lambda x: datetime.datetime.strptime(x[1], "%d. %b %Y"))))

Now I get a TypeError: must be string, not int

Community
  • 1
  • 1
kristel
  • 23
  • 4

1 Answers1

2

You want x[0] rather than x[1]:

daten, werte = (list(t) for t in zip(*sorted(zip(daten, werte), key=lambda x : datetime.datetime.strptime(x[0], "%d. %b %Y"))))
NPE
  • 486,780
  • 108
  • 951
  • 1,012