Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'next,': 0.05; 'numerical': 0.05; 'python': 0.08; 'attribute': 0.09; '[];': 0.16; 'coerced': 0.16; 'copyright,': 0.16; 'elements,': 0.16; 'looping': 0.16; 'need:': 0.16; 'received:10.127.143.52': 0.16; 'subject:list': 0.16; '(which': 0.20; 'subject:question': 0.21; 'this?': 0.22; 'loop': 0.22; 'dictionary': 0.23; '-----': 0.23; 'index': 0.25; '(or': 0.25; 'xml': 0.25; 'thanks.': 0.26; "i'm": 0.27; 'sort': 0.28; 'elements': 0.29; 'order.': 0.29; 'example': 0.30; 'array': 0.30; 'preceding': 0.30; 'received:10.127': 0.30; 'received:10.127.143': 0.30; 'received:75.180': 0.30; 'received:75.180.132': 0.30; 'received:cdptpa-omtalb.mail.rr.com': 0.30; 'cases': 0.32; 'does': 0.32; 'to:addr:python-list': 0.34; 'there': 0.34; 'project': 0.35; 'like:': 0.35; 'ordered': 0.35; 'received:mail.rr.com': 0.35; 'question': 0.35; 'similar': 0.37; 'assuming': 0.37; 'element': 0.37; 'received:75': 0.37; 'but': 0.37; 'something': 0.38; 'put': 0.38; 'received:rr.com': 0.39; 'to:addr:python.org': 0.39; 'might': 0.39; "i'd": 0.40; 'where': 0.40; 'easily': 0.61; 'from:no real name:2**0': 0.62; 'order': 0.62; 'needing': 0.64; 'address,': 0.71; 'jay': 0.84 Authentication-Results: cdptpa-omtalb.mail.rr.com smtp.user=jyoung79@kc.rr.com; auth=pass (LOGIN) X-Authority-Analysis: v=1.1 cv=aeMH4JcVOnVr0LmJAzqEvfnmJyuaZufWdlng4HTRGCk= c=1 sm=0 a=IkcTkHD0fZMA:10 a=8b3wTVuTiay1QlrVogkA:9 a=k673TY4PXWr1gZTcG0QA:7 a=QEXdDO2ut3YA:10 a=oJUCUcVVWTwdzsAs:21 a=oParoZydUh8a7AMG:21 a=KMr8SRDwdKKXQwftM2uIcw==:117 X-Cloudmark-Score: 0 Date: Sun, 17 Jul 2011 16:28:44 +0000 From: To: python-list@python.org Subject: Ordered list question MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) Sensitivity: Normal X-Originating-IP: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1310920202 news.xs4all.nl 23952 [2001:888:2000:d::a6]:35767 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:9715 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:
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