Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #9715

Ordered list question

Date 2011-07-17 16:28 +0000
From <jyoung79@kc.rr.com>
Subject Ordered list question
Newsgroups comp.lang.python
Message-ID <mailman.1174.1310920202.1164.python-list@python.org> (permalink)

Show all headers | View raw


I'm currently working on a project where I'm looping through xml elements, 
pulling the 'id' attribute (which will be coerced to a number) as well as the 
element tag.  I'm needing these elements in numerical order (from the id).  
Example xml might look like:

<price id="5">
<copyright id="1">
<address id="3">

There will be cases where elements might be excluded, but I'd still need to 
put what I find in id numerical order.  In the above example I would need 
the order of 1, 3, 5 (or copyright, address, price).  In javascript I can easily 
index an array, and any preceding elements that don't exist will be set to 
'undefined':

-----
var a = [];

a[parseInt('5')] = 'price';
a[parseInt('1')] = 'copyright';
a[parseInt('3')] = 'address';

//  a is now   [undefined, copyright, undefined, address, undefined, price]
-----

Next, I can loop through the array and remove every 'undefined' in order to 
get the ordered array I need:

-----
> var newA = [];

> for (var x = 0; x < a.length; x++) { 
    if (a[x] != undefined) { 
        newA.push(a[x]); 
    }
}

// newA is now   [copyright, address, price]
-----

My question is, does python have a similar way to do something like this?  
I'm assuming the best way is to create a dictionary and then sort it by 
the keys?

Thanks.

Jay

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Ordered list question <jyoung79@kc.rr.com> - 2011-07-17 16:28 +0000
  Re: Ordered list question Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-07-17 21:00 +0200

csiph-web