-1

I have 2 lists : result, occurredtimes I want to sort them based on the occurredtimes

These are printed outputs of the lists :

    result is        ['3 - 1', '1 - 2', '0 - 0', '2 - 1', '2 - 3', '2 - 0' ...]
    occurredtimes is [322 , 423, 269, 643, 114, 565 ...]

The output I need is

    result           ['2 - 1', '2 - 0', '1 -2', '3 - 1', '0 - 0', '2 - 3' ...]
    occurredtimes    [643 , 565, 423, 322, 269, 114 ...]

I am trying to sort them but I cannot get what I need; I am sorry, I am sure it is an easy code

I tried as follows

    result, occurredtimes = (list(t) for t in zip(*sorted(zip(result, occurredtimes))))
Al Pan
  • 39
  • 7
  • What have you tried, and what exactly is the problem with it? – jonrsharpe May 15 '20 at 15:24
  • Have you tried it yourself or are you asking generally how to sort a list? – tygzy May 15 '20 at 15:25
  • Probably a better duplicate target: [Is it possible to sort two lists(which reference each other) in the exact same way?](https://stackoverflow.com/q/9764298/7851470) – Georgy May 15 '20 at 15:49

6 Answers6

1

Try:

result, occurredtimes=zip(*sorted(zip(result, occurredtimes), key=lambda k: k[1], reverse=True))

Outputs:

('2 - 1', '2 - 0', '1 - 2', '3 - 1', '0 - 0', '2 - 3')
(643, 565, 423, 322, 269, 114)
Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
1

you can use the sorted() and pass an lambda function to sort them on the occurrence times.

In [9]: result = ['3 - 1', '1 - 2', '0 - 0', '2 - 1', '2 - 3', '2 - 0' ]

In [10]: ocurredtimes= [322 , 423, 269, 643, 114, 565]

In [11]: new_res=list( zip( result, ocurredtimes))


In [13]: new_res
Out[13]:
[('3 - 1', 322),
 ('1 - 2', 423),
 ('0 - 0', 269),
 ('2 - 1', 643),
 ('2 - 3', 114),
 ('2 - 0', 565)]


In [15]: sorted(new_res, key=lambda x:x[1],reverse=True) #sorted OUTPUT 
Out[15]:
[('2 - 1', 643),
 ('2 - 0', 565),
 ('1 - 2', 423),
 ('3 - 1', 322),
 ('0 - 0', 269),
 ('2 - 3', 114)]

Also you can use the sort function of list's and pass the same lambda function as parameter. That would do the job.

For more 1 2

teddcp
  • 1,514
  • 2
  • 11
  • 25
0
>>> result, occurredtimes = zip(*sorted(zip(result, occurredtimes), key = lambda i: i[1], reverse=True))
>>> result
('2 - 1', '2 - 0', '1 - 2', '3 - 1', '0 - 0', '2 - 3')
>>> occurredtimes
(643, 565, 423, 322, 269, 114)

The way this works is you can essentially zip the lists together element-wise. Then you can sort by the [1] element which is from occurredtimes, and use reverse=True to sort descending.

>>> sorted(zip(result, occurredtimes), key = lambda i: i[1], reverse=True)
[('2 - 1', 643), ('2 - 0', 565), ('1 - 2', 423), ('3 - 1', 322), ('0 - 0', 269), ('2 - 3', 114)]

The last zip(*sorted( is to unpack the sorted tuples back into two separate lists again.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
0

You can first combine the two list into one list and then sort based on the criteria you need. The code will look like this:

result = ['3 - 1', '1 - 2', '0 - 0', '2 - 1', '2 - 3', '2 - 0']
occurredtimes = [322 , 423, 269, 643, 114, 565]
new_list=[]
for i in range(len(result)):
    new_list.append([occurredtimes[i],result[I]])   
new_list.sort(key=lambda x:x[0],reverse=True)
print(new_list)
ksohan
  • 1,165
  • 2
  • 9
  • 23
0

I would use argsort of numpy. It's faster and less code:

O = np.array([322 , 423, 269, 643, 114, 565])
values = np.array(['3 - 1', '1 - 2', '0 - 0', '2 - 1', '2 - 3', '2 - 0'])
arr = np.argsort(O)[::-1]
print(values[arr])

Output:

array(['2 - 1', '2 - 0', '1 - 2', '3 - 1', '0 - 0', '2 - 3'], dtype='<U5')
Code Pope
  • 5,075
  • 8
  • 26
  • 68
0
occurredtimes = np.argsort(occurredtimes)[::-1]

print("result:", [ result[i] for i in occurredtimes ])
print("occuredtimes:", occurredtimes)
ddaedalus
  • 131
  • 5