Doubt regarding sorting of a list specific field

P

praba kar

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
Go to: http://yahoo.shaadi.com/india-matrimony
 
V

vincent wehren

| 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
| Go to: http://yahoo.shaadi.com/india-matrimony
 
S

Steven Bethard

praba said:
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/general.html#why-are-there-separate-tuple-and-list-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
 
F

F. Petitjean

Le Wed, 13 Apr 2005 01:13:40 -0600, Steven Bethard a écrit :
praba said:
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.
 
S

Steven Bethard

vincent said:
| 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
 
S

Steven Bethard

F. Petitjean said:
Le Wed, 13 Apr 2005 01:13:40 -0600, Steven Bethard a écrit :
praba said:
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')]
instead of
[(1234,'name3'), (1234,'name1')]
which is what I assumed the OP wanted when he said "I want to sort only
numeric value".

STeVe
 
S

Steven Bethard

Steven said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top