+ 1

How to print corresponding value of list2 in list1 when I have duplicate values?

I am done with the logic but my code prints only for 1st occuring index. l=[10,36,54,89,12,17] m=[3,7,3,0,7,0] res=sorted(m) for i in res: print(l[m.index(i)],i) I need 89 0 17 0 10 3 54 3 12 7 36 12 but my output is 89 0 89 0 10 3 10 3 36 7 36 7 help me out plz

18th Jul 2020, 5:32 PM
uma
uma - avatar
11 Respostas
+ 2
uma Sry for late response, Iam not good at python but got this way finally.... This works..(from list no element is perfect actually, is it OK?).. (printing without sort, since res have dublicates) for i in range(len(m)): print(l[i],m[i]) Next, by zip sorting n=list(zip(m,l)) n=sorted(n) for i in range(len(n)) : print(n[i]) I got this way... But working to get as required way... Edit: Oma Falk thanks for that sorting key.. Finally got this way.. https://code.sololearn.com/cDq7E36wD021/?ref=app
18th Jul 2020, 7:01 PM
Jayakrishna 🇼🇳
18th Jul 2020, 6:41 PM
Oma Falk
Oma Falk - avatar
0
I've now edited so that u can understand. Jayakrishna🇼🇳
18th Jul 2020, 5:54 PM
uma
uma - avatar
0
I want to print corresponding value of elements of elements in L with respect to elements in res
18th Jul 2020, 5:55 PM
uma
uma - avatar
0
Thanks Oma Falk
18th Jul 2020, 6:44 PM
uma
uma - avatar
0
uma you need a sort key in your solution d=sorted(c,key = lambda x: (x[0],a.index(x[1])) )
18th Jul 2020, 6:53 PM
Oma Falk
Oma Falk - avatar
0
Jayakrishna🇼🇳 for el in n: print(*el)
18th Jul 2020, 7:04 PM
Oma Falk
Oma Falk - avatar
19th Jul 2020, 8:36 AM
uma
uma - avatar
- 1
Can you explain the logic bit more.. Edit : OK. thanks for edit.......
18th Jul 2020, 5:45 PM
Jayakrishna 🇼🇳