-3

I'm working on a code that generates two lists. say for ex -

L1 = ['a', 'b', 'c']
L2 = [22, 21, 23]

How can i associate both the lists L1 and L2. I need to arrange L1 based on L2

For ex - if L2 is increasing L1 should be -

L1 = ['b', 'a', 'c']

if L2 is decreasing L1 should be -

L2 = ['c', 'a', 'b']

and so on..

hmm
  • 37
  • 8
  • I believe I understand what you want, but your example is rather confusing at this point (*IMO*) – miradulo Apr 09 '15 at 15:21
  • See previous answer [here](http://stackoverflow.com/questions/6618515/sorting-list-based-on-values-from-another-list) – Daniel Marasco Apr 09 '15 at 15:21
  • What are `a`, `b`, and `c`? Are these variables or do you mean strings `'a'`, `'b'`, and `'c'`? –  Apr 09 '15 at 15:21
  • @Tichodroma yes they are strings – hmm Apr 09 '15 at 15:24
  • Then please [edit your question](http://stackoverflow.com/posts/29542554/edit) and make them strings. –  Apr 09 '15 at 15:25

1 Answers1

3

zip the lists together, sort them, then unzip.

L1 = ['a', 'b', 'c']
L2 = [22, 21, 23]
x = zip(L2, L1)
x.sort()
L1 = zip(*x)[1]
print L1

Result:

('b', 'a', 'c')
Kevin
  • 74,910
  • 12
  • 133
  • 166