How does your "2D array" look like?
For example:
>>> a = [
[12, 18, 6, 3],
[ 4, 3, 1, 2],
[15, 8, 9, 6]
]
>>> a.sort(key=lambda x: x[1])
>>> a
[[4, 3, 1, 2],
[15, 8, 9, 6],
[12, 18, 6, 3]]
But I guess you want something like this:
>>> a = [
[12, 18, 6, 3],
[ 4, 3, 1, 2],
[15, 8, 9, 6]
]
>>> a = zip(*a)
>>> a.sort(key=lambda x: x[1])
>>> a
[(6, 1, 9),
(3, 2, 6),
(18, 3, 8),
(12, 4, 15)]
>>> a = zip(*a)
>>> a
[(6, 3, 18, 12),
(1, 2, 3, 4),
(9, 6, 8, 15)
]
Answer from Messa on Stack OverflowHow does your "2D array" look like?
For example:
>>> a = [
[12, 18, 6, 3],
[ 4, 3, 1, 2],
[15, 8, 9, 6]
]
>>> a.sort(key=lambda x: x[1])
>>> a
[[4, 3, 1, 2],
[15, 8, 9, 6],
[12, 18, 6, 3]]
But I guess you want something like this:
>>> a = [
[12, 18, 6, 3],
[ 4, 3, 1, 2],
[15, 8, 9, 6]
]
>>> a = zip(*a)
>>> a.sort(key=lambda x: x[1])
>>> a
[(6, 1, 9),
(3, 2, 6),
(18, 3, 8),
(12, 4, 15)]
>>> a = zip(*a)
>>> a
[(6, 3, 18, 12),
(1, 2, 3, 4),
(9, 6, 8, 15)
]
Python, per se, has no "2d array" -- it has (1d) lists as built-ins, and (1d) arrays in standard library module array. There are third-party libraries such as numpy which do provide Python-usable multi-dimensional arrays, but of course you'd be mentioning such third party libraries if you were using some of them, rather than just saying "in Python", right?-)
So I'll assume that by "2d array" you mean a list of lists, such as:
lol = [ range(10), range(2, 12), range(5, 15) ]
or the like -- i.e. a list with 3 items, each item being a list with 10 items, and the "second row" would be the sublist item lol[1]. Yeah, lots of assumptions, but your question is so maddeningly vague that there's no way to avoid making assumptions - edit your Q to clarify with more precision, and an example!, if you dislike people trying to read your mind (and probably failing) as you currently make it impossible to avoid.
So under these assumptions you can sort each of the 3 sublists in the order required to sort the second one, for example:
indices = range(10)
indices.sort(key = lol[1].__getitem__)
for i, sublist in enumerate(lol):
lol[i] = [sublist[j] for j in indices]
The general approach here is to sort the range of indices, then just use that appropriately sorted range to reorder all the sublists in play.
If you actually have a different problem, there will of course be different solutions;-).
How would you sort a 2D list by row based on the first column using a separate list as a key
python - How to sort multidimensional array by column? - Stack Overflow
Sorting 2d Array
Sorting 2d array by rows in python - Stack Overflow
so i have a data set of rain data over the course of a year where it ends up being formatted into something like this except its a lot longer
[['Nov', 0.0], ['Nov', 0.0], ['Nov', 0.09],['Feb', 0.0], ['Feb', 0.0], ['Feb', 0.09],['Oct', 0.56], ['Oct', 0.0], ['Oct', 0.03], ['Oct', 0.62]]
now I figured I would need another list to function as a lookup to help sort this data by month according to calendar order so I made one
month_lookup = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
but now I am a tad confused about how I would go about using something like the sorted() function to use the lookup as the way to sort the 2D list above
Yes. The sorted built-in accepts a key argument:
sorted(li,key=lambda x: x[1])
Out[31]: [['Jason', 1], ['John', 2], ['Jim', 9]]
note that sorted returns a new list. If you want to sort in-place, use the .sort method of your list (which also, conveniently, accepts a key argument).
or alternatively,
from operator import itemgetter
sorted(li,key=itemgetter(1))
Out[33]: [['Jason', 1], ['John', 2], ['Jim', 9]]
Read more on the python wiki.
You can use the sorted method with a key.
sorted(a, key=lambda x : x[1])
Regular sorted does it:
print(sorted([[2,3,1,8], [4,7,5,20], [0,-2,2,0]]))
But if you only want to sort by the first columns, use:
print(sorted([[2,3,1,8], [4,7,5,20], [0,-2,2,0]], key=lambda x: x[0]))
They both output:
[[0, -2, 2, 0], [2, 3, 1, 8], [4, 7, 5, 20]]
If you intend to get numpy operation:
arr = arr[arr[:,0].argsort()]
A first step could be to create a nested list, adding every 2 elements to a new sublist:
from itertools import chain
from operator import itemgetter
i = [([0.2,0.10]),0.69, ([0.3,0.67]),0.70, ([0.5,0.68]),0.70, ([0.3,0.67]),0.65]
l = [i[x:x+2] for x in range(0, len(i),2)]
# [[[0.2, 0.1], 0.69], [[0.3, 0.67], 0.7], [[0.5, 0.68], 0.7], [[0.3, 0.67], 0.65]]
And then sort the nested list by the second element in each sublist with operator.itemgetter, and use itertools.chain to flatten the result:
list(chain(*sorted(l, key = itemgetter(1), reverse=True)))
[[0.3, 0.67], 0.7, [0.5, 0.68], 0.7, [0.2, 0.1], 0.69, [0.3, 0.67], 0.65]
Another approach with zip and sorting with a lambda sorting key:
First, put your "third position" in a tuple with the first and second number, using zip:
output = list(zip(output[::2], output[1::2]))
#[([0.3, 0.67], 0.7), ([0.5, 0.68], 0.7), ([0.2, 0.1], 0.69), ([0.3, 0.67], 0.65)]
And then sort, using your third number (in the tuple it's on position 2) as the sorting key:
output.sort(key = lambda x: x[1])
#[([0.3, 0.67], 0.65), ([0.2, 0.1], 0.69), ([0.3, 0.67], 0.7), ([0.5, 0.68], 0.7)]
Use .argsort() it returns an numpy.array of indices that sort the given numpy.array. You call it as a function or as a method on your array. For example, suppose you have
import numpy as np
arr = np.array([[-0.30565392, -0.96605562],
[ 0.85331367, -2.62963495],
[ 0.87839643, -0.28283675],
[ 0.72676698, 0.93213482],
[-0.52007354, 0.27752806],
[-0.08701666, 0.22764316],
[-1.78897817, 0.50737573],
[ 0.62260038, -1.96012161],
[-1.98231706, 0.36523876],
[-1.07587382, -2.3022289 ]])
You can now call .argsort() on the column you want to sort, and it will give you an array of row indices that sort that particular column which you can pass as an index to your original array.
>>> arr[arr[:, 1].argsort()]
array([[ 0.85331367, -2.62963495],
[-1.07587382, -2.3022289 ],
[ 0.62260038, -1.96012161],
[-0.30565392, -0.96605562],
[ 0.87839643, -0.28283675],
[-0.08701666, 0.22764316],
[-0.52007354, 0.27752806],
[-1.98231706, 0.36523876],
[-1.78897817, 0.50737573],
[ 0.72676698, 0.93213482]])
You can equivalently use numpy.argsort()
>>> arr[np.argsort(arr[:, 1])]
array([[ 0.85331367, -2.62963495],
[-1.07587382, -2.3022289 ],
[ 0.62260038, -1.96012161],
[-0.30565392, -0.96605562],
[ 0.87839643, -0.28283675],
[-0.08701666, 0.22764316],
[-0.52007354, 0.27752806],
[-1.98231706, 0.36523876],
[-1.78897817, 0.50737573],
[ 0.72676698, 0.93213482]])
sorted(Data, key=lambda row: row[1]) should do it.