Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Sorting a multidimensional array by multiple keys
- From: attn.steven.kuo at gmail.com (attn.steven.kuo at gmail.com)
- Subject: Sorting a multidimensional array by multiple keys
- Date: 31 Mar 2007 09:14:13 -0700
On Mar 31, 6:42 am, Rehceb Rotkiv <reh... at no.spam.plz> wrote:
(snipped)
> As each line consists of 5 words, I would break up the data into an array
> of five-field-arrays (Would you use lists or tuples or a combination in
> Python?). The word "BUT" would be in the middle, with two fields/words
> left and two fields/words right of it. I then want to sort this list by
>
> - field 3
> - field 4
> - field 1
> - field 0
import StringIO
buf = """
reaction is BUT by the
sodium , BUT it is
sea , BUT it is
this manner BUT the dissolved
pattern , BUT it is
rapid , BUT it is
""".lstrip()
mockfile = StringIO.StringIO(buf)
tokens = [ line.split() + [ line ] for line in mockfile ]
tokens.sort(key=lambda l: (l[3], l[4], l[1], l[0]))
for l in tokens:
print l[-1],
--
Hope this helps,
Steven
- References:
- Sorting a multidimensional array by multiple keys
- From: Rehceb Rotkiv
- Sorting a multidimensional array by multiple keys
- From: bearophileHUGS at lycos.com
- Sorting a multidimensional array by multiple keys
- From: Rehceb Rotkiv
- Sorting a multidimensional array by multiple keys
- Prev by Date: What are OOP's Jargons and Complexities
- Next by Date: os.system questions
- Previous by thread: Sorting a multidimensional array by multiple keys
- Next by thread: Sorting a multidimensional array by multiple keys
- Index(es):