views:

303

answers:

11

I want to sort the array c. But I don't get the answer a,b,c,d. Instead I get a,b,d,c. What could I do, for sorting the whole array and not only one row?

EDIT: I want to sort the numbers. And the connected letters, should have the same order like the sorted numbers. sorry my question wasn't clear. Maybe I should join number and letters first. Like this: [['a',1]['b',2]....

a = ['a','b','d','c']
b = [1,2,4,3]
c = [[],[]]
c[0]=a
c[1]=b
c[1].sort()
print(c)
+1  A: 
[sorted(x) for x in c]
Ignacio Vazquez-Abrams
sorry but it doesn't work. :/
kame
Of course it does. It just doesn't work the way you think it does. It returns a new list containing sorted lists.
Ignacio Vazquez-Abrams
okay, but i want to sort the numbers. and the connected letters. should have the same order like the numbers. sorry my question wasn't clear.
kame
... That didn't really help...
Ignacio Vazquez-Abrams
+1, @kame: explain what do you expect if not this?
Antony Hatchkins
sorry for that. :(
kame
please see above, I get the solution
kame
+4  A: 

Let's take a look at what's going on here:

# Initialize the lists
a = ['a','b','d','c']
b = [1,2,4,3]
c = [[],[]]

# Assign the lists to positions in c
c[0]=a
c[1]=b

# Sort b, which was assigned to c[1]
c[1].sort()
print(c)

So, of course you could not expect a to get sorted. Try this instead:

# Sort a, which was assigned to c[0]
c[0].sort()

# Sort b, which was assigned to c[1]
c[1].sort()
print(c)

Or if c is of variable length:

# Sort every list in c
for l in c:
    l.sort()

Edit: in response to your comment, the letters are not connected to the numbers in any way. If you want them to be connected, you need to join them in a structure like a tuple. Try:

>>> c = [ (1, 'a'), (2, 'b'), (4, 'd'), (3, 'c') ]
>>> c.sort()
>>> print c 
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

By default, tuples will sort on their first element. Note that you could use any letters here in place of a, b, c, d, and the tuples would still sort the same (by number).

danben
Now I see the problem. Maybe I should join the number and the letter at first.
kame
when I sort the numbers, the connected letters should join too.
kame
+3  A: 

This appears to be what you really want to do:

>>> a = ['a', 'z', 'd', 'c']
>>> b = [1,   2,   4,   3]
>>> c = zip(a, b)
>>> c
[('a', 1), ('z', 2), ('d', 4), ('c', 3)]
>>> import operator
>>> c.sort(key=operator.itemgetter(1))
# this would be equivalent: c.sort(key=lambda x: x[1])
>>> c
[('a', 1), ('z', 2), ('c', 3), ('d', 4)]
Roger Pate
but now he sorts the letters. I want to sort the numbers.
kame
then: c = zip(b, a)
Tomasz Zielinski
so zip(b,a) instead
BlueRaja - Danny Pflughoeft
kame: Sorry about that, fixed now; I see you've edited the question to be clear on that point, too. :)
Roger Pate
A: 

In Python, an array (of the sort you are using) is called list. As for your problem, change c[1].sort() to c[0].sort() and your list of strings will be sorted instead of the list of ints contained in c[1].

MAK
but I want to sort the numbers. -- array --> list ! okay
kame
Oh, I guess danben's answer has solved it for your now :). And I just pointed out the array vs. list thing because the term array is used in Python to describe another, less flexible but faster, container (http://docs.python.org/library/array.html).
MAK
+1  A: 

The first thing that comes to mind for me on this is to use the numpy array, rather than the builtin list datatype.

Something like:

>>> from numpy import *
>>> a = array(['a', 'b', 'd', 'c'])
>>> a.sort()
>>> print a
['a' 'b' 'c' 'd']
>>> reshape(a, (2,2))
array([['a', 'b'],
       ['c', 'd']], 
      dtype='|S1')
KingRadical
I think that's a bit of overkill for this problem.
Adriano Varoli Piazza
+1  A: 
def sort_parallel(a, b):
    ba = zip(b, a)
    ba.sort()
    return [e[1] for e in ba]

a = ['a','b','d','c']
b = [1,2,4,3]

print sort_parallel(a, b)

prints

['a', 'b', 'c', 'd']
Ned Batchelder
+1  A: 
>>> a = ['a','b','d','c']
>>> b = [1, 2, 4, 3]
>>> c = zip(a, b)
>>> c
[('a', 1), ('b', 2), ('d', 4), ('c', 3)]
>>> c.sort(key=lambda x: x[1])
>>> c
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Antony Hatchkins
A: 

Roger Pate gave a good answer, but you said "but now he sorts the letters. I want to sort the numbers."

Here is a modified version of Roger Pate's answer that sorts c by the numbers. Is this what you want?

>>> def mykey(tup):
>>>     return tup[1]
>>>
>>> a = ['a','b','d','c']
>>> b = [1, 2, 4, 3]
>>> c = zip(a, b)
>>> c
[('a', 1), ('b', 2), ('d', 4), ('c', 3)]
>>> c.sort(key=mykey)
>>> c
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

The "key" to the sort() method function is a function. The function returns the key you want to use. The mykey() function takes a tuple and returns its second value (value indexed by 1). Thus, .sort() will sort using the number part of the tuple. And the strings will still match the numbers. You could even split the list c again to recover lists a and b, and they will still match.

steveha
A: 
aaa
+1  A: 

You could try (for Python 3.x):

def sort_a_based_on_b(a, b):
    c = sorted(list(zip(b, a)))
    return list(list(zip(*c))[1]) # Returns the sorted a

This returns the sorted a, based on the values in b.

a = ['a','b','d','c']
b = [1,2,4,3]

print(sort_a_based_on_b(a,b))

Prints ['a', 'b', 'c', 'd']

Reshure
A: 

Is this what you're after?

>>> a = ['a','b','d','c']
>>> b = [1, 2, 4, 3]
>>> c = zip(b, a)
>>> c.sort()
>>> c = [(y, x) for (x, y) in c]
>>> print(c)
[('a', 1), ('b', 2), ('c', 3), ('d', 4)]
Gareth Williams