Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort different lists in the same sequence

I have three different stripes and I need to sort them by ‘num’

cod = []
desc = []
num =[]

cod.append("1")
desc.append("Product1")
num.append(94)

cod.append("2")
desc.append("Product2")
num.append(93)

cod.append("3")
desc.append("Product3")
num.append(95)

for row in range(len(cod)):
    print(cod[row], desc[row], num[row])

The result I want to get is this

3 Product3 95
1 Product1 94
2 Product2 93

I tried using num.sort() but it didn’t work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

cod = []
desc = []
num =[]

cod.append("1")
desc.append("Product1")
num.append(94)

cod.append("2")
desc.append("Product2")
num.append(93)

cod.append("3")
desc.append("Product3")
num.append(95)

zipped = list(zip(num, cod, desc))
zipped.sort()

num, desc, cod = zip(*zipped)

for row in range(len(cod)):
    print(cod[row], desc[row], num[row])

Output:

Product2 2 93
Product1 1 94
Product3 3 95
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading