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
>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