468,761 Members | 1,712 Online

# Doubt regarding sorting of a list specific field

Dear All,

I have doubt regarding sorting. I have a list
that list have another list (eg)

list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

I want to sort only numeric value having array field.
How I need to do for that.

with regards
Prabahar

__________________________________________________ ______________________
Yahoo! India Matrimony: Find your life partner online
Jul 18 '05 #1
6 1801
"praba kar" <pr*********@yahoo.co.in> schrieb im Newsbeitrag
news:ma**************************************@pyth on.org...
| Dear All,
|
| I have doubt regarding sorting. I have a list
| that list have another list (eg)
|
| list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

-> Be careful, 0432 is octal notation for 282.
|
| I want to sort only numeric value having array field.
| How I need to do for that.

You may want use the decorate-sort-undecorate idiom.
I othere words, you isolate the index you want to sort by,
sort on the indices, than get rid of the indices again.

Something like:

def sortSeqOfSeqs(seq, idx):
tmp = sorted([(elem[idx], elem) for elem in seq])
return [elem[1] for elem in tmp]

seq = [[1234,'name1'],[2234,'name2'],[1432,'name3']]
print sortSeqOfSeqs(seq, 0)
# prints [[1234, 'name1'], [1432, 'name3'], [2234, 'name2']]
# Or to sort by the name index
print sortSeqOfSeqs(seq, 1)
# prints [[1234, 'name1'], [2234, 'name2'], [1432, 'name3']]
Is this what you we're looking for?

--

Vincent Wehren

|
| with regards
| Prabahar
|
|
|
|
|
|
| __________________________________________________ ______________________
| Yahoo! India Matrimony: Find your life partner online
Jul 18 '05 #2
praba kar wrote:
I have doubt regarding sorting. I have a list
that list have another list (eg)

list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

I want to sort only numeric value having array field.
How I need to do for that.

In Python 2.4:

py> import operator
py> seq = [(1234,'name1'),(2234,'name2'),(1432,'name3')]
py> seq.sort(key=operator.itemgetter(0))
py> seq
[(1234, 'name1'), (1432, 'name3'), (2234, 'name2')]

Note that I've changed your name 'list' to 'seq' since 'list' is
shadowing the builtin list function in your code, and I've changed your
nested lists to tuples because they look like groups of differently
typed objects, not sequences of similarly typed objects. See:

http://www.python.org/doc/faq/genera...ist-data-types

That said, the code I've given will still work if you shadow the builtin
list or if you use lists instead of tuples.

STeVe
Jul 18 '05 #3
Le Wed, 13 Apr 2005 01:13:40 -0600, Steven Bethard a écrit :
praba kar wrote:
list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

I want to sort only numeric value having array field.
How I need to do for that.

In Python 2.4:

py> import operator
py> seq = [(1234,'name1'),(2234,'name2'),(1432,'name3')]
py> seq.sort(key=operator.itemgetter(0))
py> seq
[(1234, 'name1'), (1432, 'name3'), (2234, 'name2')]

Note that I've changed your name 'list' to 'seq' since 'list' is
shadowing the builtin list function in your code, and I've changed your
nested lists to tuples because they look like groups of differently
typed objects, not sequences of similarly typed objects. See:

STeVe

nice explaination. But, if the items of the list are tuples the default
rule for comparing tuples is such that
seq.sort()
would be sufficient here.

Jul 18 '05 #4
vincent wehren wrote:
"praba kar" <pr*********@yahoo.co.in> schrieb im Newsbeitrag
news:ma**************************************@pyth on.org...
| Dear All,
|
| I have doubt regarding sorting. I have a list
| that list have another list (eg)
|
| list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

-> Be careful, 0432 is octal notation for 282.
|
| I want to sort only numeric value having array field.
| How I need to do for that.

You may want use the decorate-sort-undecorate idiom.
I othere words, you isolate the index you want to sort by,
sort on the indices, than get rid of the indices again.

Something like:

def sortSeqOfSeqs(seq, idx):
tmp = sorted([(elem[idx], elem) for elem in seq])
return [elem[1] for elem in tmp]

Note that this is not stable:

py> seq = [(1,'c'),(1,'b')]
py> [tup[1] for tup in sorted((x[0], x) for x in seq)]
[(1, 'b'), (1, 'c')]

Which should not have reordered the list since 1 == 1. If you want a
stable version, you could use:

py> [tup[2] for tup in sorted((x[0], i, x) for i, x in enumerate(seq))]
[(1, 'c'), (1, 'b')]

But that seems pretty silly. You're already using Python 2.4 features
(e.g. sorted), so you might as well use the key argument to sorted:

py> sorted(seq, key=operator.itemgetter(0))
[(1, 'c'), (1, 'b')]

Note if you're not using 2.4, you may want to use the
decorate-sort-undecorate pattern like:

py> decorated_seq = [(x[0], i, x) for i, x in enumerate(seq)]
py> decorated_seq.sort()
py> [tup[2] for tup in decorated_seq]
[(1, 'c'), (1, 'b')]

But personally, I'd just upgrade. ;)

STeVe
Jul 18 '05 #5
F. Petitjean wrote:
Le Wed, 13 Apr 2005 01:13:40 -0600, Steven Bethard a écrit :
praba kar wrote:
list = [[1234,'name1'],[2234,'name2'],[0432,'name3']]

I want to sort only numeric value having array field.
How I need to do for that.

In Python 2.4:

py> import operator
py> seq = [(1234,'name1'),(2234,'name2'),(1432,'name3')]
py> seq.sort(key=operator.itemgetter(0))
py> seq
[(1234, 'name1'), (1432, 'name3'), (2234, 'name2')]

Note that I've changed your name 'list' to 'seq' since 'list' is
shadowing the builtin list function in your code, and I've changed your
nested lists to tuples because they look like groups of differently
typed objects, not sequences of similarly typed objects. See:

STeVe

nice explaination. But, if the items of the list are tuples the default
rule for comparing tuples is such that
seq.sort()
would be sufficient here.

See my other post in this thread. seq.sort() is not stable; if the OP's
list looked like:
[(1234,'name3'), (1234,'name1')]
then calling seq.sort() would give
[(1234,'name1'), (1234,'name3')]
[(1234,'name3'), (1234,'name1')]
which is what I assumed the OP wanted when he said "I want to sort only
numeric value".

STeVe
Jul 18 '05 #6
Steven Bethard wrote:
See my other post in this thread. seq.sort() is not stable;

Hmmm... That didn't come out right. list.sort *is* stable. It's just
that if you want to sort only by the first element of each tuple in a
list, then list.sort won't produce results that are stable under this
criterion.

STeVe
Jul 18 '05 #7

### This discussion thread is closed

Replies have been disabled for this discussion.