vote up 0 vote down star

how to sort using 3 parallel array lists:

num1 = ['a','b','c,']
num2 = ['apple','pear','grapes']
num3 = [2.5,4.0,.68]

I used 2 for statements followed by a if statement. Sorting by elements the output should be:

a apple 2.5
b pear 4.0
c grapes .68

unfortunately, I am having issues with sorting the 3rd num3 values using the element swapping sort. any ideas

flag
1  
It looks like you are interleaving the lists. Is there sorting involved? – Ned Batchelder Nov 13 at 3:06
2  
Is this homework? – tom10 Nov 13 at 3:12
-1: Parallel lists are a mistake (unless, of course, this is homework). Why don't you merge them into tuples first? – S.Lott Nov 13 at 12:25

2 Answers

vote up 2 vote down

Since you say the lists are parallel, let's group them into tuples, and then sort the list of tuples.

num1 = ['a','b','c']
num2 = ['apple','pear','grapes']
num3 = [2.5,4.0,.68]

lst = zip(num1, num2, num3)
lst.sort()

for x1, x2, x3 in lst:
    print x1, x2, x3,

print

The result is:

a apple 2.5 b pear 4.0 c grapes 0.68

link|flag
vote up 1 vote down

From your desired inputs and output it doesn't seem you want any sorting -- just:

num1 = ['a','b','c']
num2 = ['apple','pear','grapes']
num3 = [2.5,4.0,.68]
for item in [x for t in zip(num1, num2, num3) for x in t]:
  print item,
print

This does give the output you mention -- is this what you want?

link|flag

Your Answer

Get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.