What iterable method should I use for Lists of Lists

Sayth Renshaw flebber.crue at gmail.com
Sun Apr 17 22:12:45 EDT 2016


On Monday, 18 April 2016 12:05:39 UTC+10, Sayth Renshaw wrote:
> Hi
>> I have an XML and using pyquery to obtain the elements within it and then write it to csv.
>> What is the best most reliable way to take dictionaries of each element, and print them(csv write later) based on each position so get item 0 of each list and then it 1 and so on.
>> Any other code I post is open to criticism. Because there are many attributes I will want to collect my thought is to create a list of lists, again seems a bit clunky so could be wrong.
>> from pyquery import PyQuery as pq
>>> d = pq(filename='20160319RHIL0_edit.xml')
> res = d('nomination')
> # myAt = pq.each(res.attr('bbid'))
> # print(repr(res))
> # myAt = [res.eq(i).attr('horse') for i in range(len(res))]
> # print(myAt)
>> nomID = [res.eq(i).attr('horse') for i in range(len(res))]
> horseName = [res.eq(i).attr('horse') for i in range(len(res))]
> group = [nomID, horseName]
>> for items in group:
> print(items)
>>> This is my source.
>> <?xml version="1.0" encoding="UTF-8"?>
> <meeting id="42736" barriertrial="0" venue="Rosehill Gardens" date="2016年03月19日T00:00:00" gearchanges="-1" stewardsreport="-1" gearlist="-1" racebook="0" postracestewards="0" meetingtype="TAB" rail="Timing - Electronic : Rail - +2m" weather="Fine " trackcondition="Good" nomsdeadline="2016年03月14日T11:00:00" weightsdeadline="2016年03月15日T16:00:00" acceptdeadline="2016年03月16日T09:00:00" jockeydeadline="2016年03月16日T12:00:00">
> <club abbrevname="Australian Turf Club" code="56398" associationclass="1" website="http://" />
> <race id="213262" number="1" nomnumber="7" division="0" name="NATHAN'S FAMOUS EPONA STAKES" mediumname="EPONA" shortname="EPONA STAKES" stage="Results" distance="1900" minweight="0" raisedweight="0" class="~ " age="3U " grade="0" weightcondition="SWP " trophy="1000" owner="1000" trainer="0" jockey="0" strapper="0" totalprize="150000" first="90000" second="30000" third="15000" fourth="7500" fifth="3000" time="2016年03月19日T12:40:00" bonustype=" " nomsfee="0" acceptfee="0" trackcondition="Good " timingmethod="Electronic" fastesttime="1-56.83 " sectionaltime="600/35.3 " formavailable="0" racebookprize="Of 150000ドル and trophies of 1000ドル. First 90000ドル and trophies of 1000ドル to owner, second 30000,ドル third 15000,ドル fourth 7500,ドル fifth 3000,ドル sixth 1500,ドル seventh 1500,ドル eighth 1500ドル">
> <condition line="1">Of 150000ドル and trophies of 1000ドル. First 90000ドル and trophies of 1000ドル to owner, second 30000,ドル third 15000,ドル fourth 7500,ドル fifth 3000,ドル sixth 1500,ドル seventh 1500,ドル eighth 1500ドル</condition>
> <condition line="3">No class restriction, Set Weights plus Penalties, For Three-Years-Old and Upwards, Fillies and Mares, (Group 3)</condition>
> <condition line="5">No Allowances for apprentices. Field Limit: 14 + 4 EM</condition>
> <nomination number="4" saddlecloth="4" horse="Vergara" id="171115" idnumber="" regnumber="" blinkers="1" trainernumber="38701" trainersurname="Cummings" trainerfirstname="Anthony" trainertrack="Randwick" rsbtrainername="Anthony Cummings" jockeynumber="86876" jockeysurname="McDonald" jockeyfirstname="James" barrier="7" weight="55" rating="93" description="B M 5 Snippetson x Graces Spirit (Flying Spur)" colours="Yellow, Red Epaulettes And Cap" owners="Anthony Cummings Thoroughbreds Pty Ltd Syndicate (Mgrs: A & B Cummings) & P C Racing Investments Syndicate (Mgr: P J Carroll) " dob="2010年10月07日T00:00:00" age="6" sex="M" career="30-7-4-2 295445ドル.00" thistrack="6-1-1-0 90500ドル.00" thisdistance="0-0-0-0" goodtrack="17-3-2-2 101440ドル.00" heavytrack="5-0-1-0 20200ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="7-2-1-2 108340ドル.00" secondup="7-1-1-0 43200ドル.00" mindistancewin="0" maxdistancewin="0" finished="1" weightvariation="0" variedweight="55" decimalmargin="0.00" penalty="0" pricestarting="12ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="2" saddlecloth="2" horse="Heavens Above" id="187674" idnumber="" regnumber="" blinkers="0" trainernumber="736" trainersurname="Martin" trainerfirstname="Tim" trainertrack="Rosehill" rsbtrainername="Tim Martin" jockeynumber="46930" jockeysurname="Angland" jockeyfirstname="Tye" barrier="6" weight="56" rating="88" description="B M 4 Street Cry(IRE) x Reggie(NZ) (Germano(GB))" colours="Yellow, Green Chevrons, Striped Cap" owners="President Bloodstock Pty Ltd (Mgr: R C Kemister)" dob="2011年08月12日T00:00:00" age="5" sex="M" career="12-3-3-4 271520ドル.00" thistrack="4-0-3-0 41425ドル.00" thisdistance="0-0-0-0" goodtrack="10-2-2-4 214845ドル.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="2-0-1-1 20710ドル.00" secondup="2-0-2-0 24675ドル.00" mindistancewin="0" maxdistancewin="0" finished="2" weightvariation="0" variedweight="56" decimalmargin="0.20" penalty="0" pricestarting="3ドル.80F" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="6" saddlecloth="6" horse="Sweet Fire" id="184732" idnumber="" regnumber="" blinkers="0" trainernumber="681" trainersurname="Waller" trainerfirstname="Chris" trainertrack="Rosehill" rsbtrainername="Chris Waller" jockeynumber="51661" jockeysurname="Berry" jockeyfirstname="Tommy" barrier="5" weight="54" rating="85" description="BR M 4 Shamardal(USA) x Zarinia(IRE) (Intikhab(USA))" colours="Lilac And White Halves, Black Cap" owners="E J Lucas " dob="2011年09月25日T00:00:00" age="5" sex="M" career="15-4-2-2 212750ドル.00" thistrack="6-1-1-1 56690ドル.00" thisdistance="0-0-0-0" goodtrack="9-2-1-2 120395ドル.00" heavytrack="1-0-0-0 450ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-0-0-0 1900ドル.00" secondup="3-1-1-0 42200ドル.00" mindistancewin="0" maxdistancewin="0" finished="3" weightvariation="0" variedweight="54" decimalmargin="0.50" penalty="0" pricestarting="7ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="7" saddlecloth="7" horse="Alegria" id="181928" idnumber="" regnumber="" blinkers="1" trainernumber="235" trainersurname="O'Shea" trainerfirstname="John" trainertrack="Agnes Banks/Hawkesbury" rsbtrainername="John O'Shea" jockeynumber="40275" jockeysurname="Clark" jockeyfirstname="Tim" barrier="1" weight="54" rating="81" description="B M 4 Medaglia D'oro(USA) x Pure Joy (Fusaichi Pegasus(USA))" colours="Royal Blue" owners="Godolphin" dob="2011年10月06日T00:00:00" age="5" sex="M" career="12-3-1-5 216850ドル.00" thistrack="4-1-0-2 89600ドル.00" thisdistance="0-0-0-0" goodtrack="5-1-1-3 87575ドル.00" heavytrack="4-2-0-1 114275ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-2-0-1 80500ドル.00" secondup="3-0-0-2 20850ドル.00" mindistancewin="0" maxdistancewin="0" finished="4" weightvariation="0" variedweight="54" decimalmargin="0.70" penalty="0" pricestarting="8ドル.50" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="11" saddlecloth="11" horse="Piamimi" id="158914" idnumber="" regnumber="" blinkers="0" trainernumber="773" trainersurname="Bennett" trainerfirstname="Greg" trainertrack="Scone" rsbtrainername="Greg Bennett" jockeynumber="48503" jockeysurname="Williams" jockeyfirstname="Craig" barrier="8" weight="54" rating="75" description="B OR BR M 6 Not a Single Doubt x Blurrta(NZ) (Bahhare(USA))" colours="Yellow, Dark Blue Diamonds, Striped Sleeves And Cap" owners="Dr M Wylie, K Kenny, J Canning, M Mcfayden, B Hyde & P Toubji " dob="2009年08月23日T00:00:00" age="7" sex="M" career="35-6-4-3 170275ドル.00" thistrack="2-0-0-0 5150ドル.00" thisdistance="2-0-0-0 900ドル.00" goodtrack="25-3-2-2 78640ドル.00" heavytrack="4-1-1-1 24510ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="8-1-3-0 27375ドル.00" secondup="8-1-0-2 32645ドル.00" mindistancewin="0" maxdistancewin="0" finished="5" weightvariation="0" variedweight="54" decimalmargin="1.10" penalty="0" pricestarting="41ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="10" saddlecloth="10" horse="Blendwell" id="171408" idnumber="" regnumber="" blinkers="0" trainernumber="78024" trainersurname="Moody" trainerfirstname="Peter G" trainertrack="Warwick Farm" rsbtrainername="Peter G Moody" jockeynumber="85036" jockeysurname="Lane" jockeyfirstname="Damian" barrier="10" weight="54" rating="78" description="B M 5 Hard Spun(USA) x Bridie Belle(NZ) (Al Akbar)" colours="Tartan, Red Sash And Armbands" owners="D & M Esplin Pty Ltd (Mgr: Mrs M E Esplin)" dob="2010年10月06日T00:00:00" age="6" sex="M" career="17-4-3-4 107845ドル.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="8-0-1-2 25135ドル.00" heavytrack="2-2-0-0 42000ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="6-0-0-3 8835ドル.00" secondup="3-1-0-1 20050ドル.00" mindistancewin="0" maxdistancewin="0" finished="6" weightvariation="0" variedweight="54" decimalmargin="1.20" penalty="0" pricestarting="17ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="3" saddlecloth="3" horse="Adorabeel (NZ)" id="166836" idnumber="" regnumber="" blinkers="0" trainernumber="1942" trainersurname="Pride" trainerfirstname="Joseph" trainertrack="Warwick Farm" rsbtrainername="Joseph Pride" jockeynumber="86280" jockeysurname="Clipperton" jockeyfirstname="Sam" barrier="9" weight="56" rating="86" description="BR OR BLK M 6 Savabeel x Plaza Doree(FR) (Saumarez(GB))" colours="Maroon, White Crossed Sashes, Yellow Armbands And Cap" owners="Mystery Downs (Mgr: F W Cook)" dob="2009年09月11日T00:00:00" age="7" sex="M" career="34-6-5-4 281810ドル.00" thistrack="11-1-2-1 105750ドル.00" thisdistance="2-0-0-0 3800ドル.00" goodtrack="24-3-3-1 203910ドル.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="7-1-1-1 28650ドル.00" secondup="7-0-2-1 25050ドル.00" mindistancewin="0" maxdistancewin="0" finished="7" weightvariation="0" variedweight="56" decimalmargin="2.30" penalty="0" pricestarting="13ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="8" saddlecloth="8" horse="Mary Lou" id="172933" idnumber="" regnumber="" blinkers="1" trainernumber="38701" trainersurname="Cummings" trainerfirstname="Anthony" trainertrack="Randwick" rsbtrainername="Anthony Cummings" jockeynumber="86596" jockeysurname="Collett" jockeyfirstname="Jason" barrier="11" weight="54" rating="80" description="B M 5 Fastnet Rock x Miss Alabama(NZ) (Woodman(USA))" colours="Red, Black Diagonal Stripes And Armbands, Quartered Cap" owners="E-Thoroughbred (Mgr: A W & M G Bott) & Evergreen Rich (Mgr: A W Bott, P K Siu, M Siu & E Siu)" dob="2010年09月18日T00:00:00" age="6" sex="M" career="18-3-3-4 156860ドル.00" thistrack="3-1-0-1 33375ドル.00" thisdistance="1-1-0-0 17250ドル.00" goodtrack="14-3-2-4 130335ドル.00" heavytrack="2-0-0-0 525ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-1-0-0 31125ドル.00" secondup="3-0-1-2 25100ドル.00" mindistancewin="0" maxdistancewin="0" finished="8" weightvariation="0" variedweight="54" decimalmargin="2.70" penalty="0" pricestarting="18ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="9" saddlecloth="9" horse="Skyline Blush" id="182533" idnumber="" regnumber="" blinkers="1" trainernumber="38701" trainersurname="Cummings" trainerfirstname="Anthony" trainertrack="Randwick" rsbtrainername="Anthony Cummings" jockeynumber="1472" jockeysurname="McEvoy" jockeyfirstname="Kerrin" barrier="4" weight="54" rating="80" description="B M 4 Northern Meteor x Cara Y Cruz(USA) (King of Kings(IRE))" colours="Red, Black Stripes" owners="Gooree Stud (E M Cojuangco & M C Cummings)" dob="2011年10月29日T00:00:00" age="5" sex="M" career="14-5-0-2 204190ドル.00" thistrack="4-1-0-1 52225ドル.00" thisdistance="2-1-0-1 41375ドル.00" goodtrack="7-0-0-1 19535ドル.00" heavytrack="4-2-0-1 111125ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="3-1-0-0 21125ドル.00" secondup="3-0-0-1 12460ドル.00" mindistancewin="0" maxdistancewin="0" finished="9" weightvariation="0" variedweight="54" decimalmargin="3.10" penalty="0" pricestarting="9ドル.50" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="5" saddlecloth="5" horse="All Cerise" id="171801" idnumber="" regnumber="" blinkers="0" trainernumber="681" trainersurname="Waller" trainerfirstname="Chris" trainertrack="Rosehill" rsbtrainername="Chris Waller" jockeynumber="86721" jockeysurname="Dunn" jockeyfirstname="Dwayne" barrier="3" weight="54" rating="92" description="B M 5 Redoute's Choice x Trick Taker(USA) (Capote(USA))" colours="Purple, White Hoops, Black Seams, Purple And White Quartered Cap" owners="Woppitt Bloodstock (Mgr: Mrs D N Kepitis)" dob="2010年11月05日T00:00:00" age="6" sex="M" career="28-6-3-3 349890ドル.00" thistrack="10-1-1-1 111750ドル.00" thisdistance="0-0-0-0" goodtrack="21-6-2-3 327690ドル.00" heavytrack="2-0-0-0 450ドル.00" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="7-1-0-2 45790ドル.00" secondup="4-1-0-0 71200ドル.00" mindistancewin="0" maxdistancewin="0" finished="10" weightvariation="0" variedweight="54" decimalmargin="4.10" penalty="0" pricestarting="6ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> <nomination number="1" saddlecloth="1" horse="Gust of Wind (NZ)" id="181079" idnumber="" regnumber="" blinkers="1" trainernumber="41655" trainersurname="Sargent" trainerfirstname="John" trainertrack="Randwick" rsbtrainername="John Sargent" jockeynumber="84015" jockeysurname="Avdulla" jockeyfirstname="Brenton" barrier="2" weight="59" rating="106" description="B M 4 Darci Brahma(NZ) x Starrystarrynight(IRE) (Sadler's Wells(USA))" colours="Yellow, White Checked Sash, Checked Sleeves And Cap" owners="Go Bloodstock (Mgr: Sir O G Glenn)" dob="2011年09月07日T00:00:00" age="5" sex="M" career="12-3-0-0 974900ドル.00" thistrack="0-0-0-0" thisdistance="0-0-0-0" goodtrack="10-2-0-0 279150ドル.00" heavytrack="0-0-0-0" slowtrack="" deadtrack="" fasttrack="0-0-0-0" firstup="4-1-0-0 8900ドル.00" secondup="3-1-0-0 20250ドル.00" mindistancewin="0" maxdistancewin="0" finished="11" weightvariation="0" variedweight="59" decimalmargin="7.40" penalty="0" pricestarting="15ドル" sectional200="0" sectional400="0" sectional600="0" sectional800="0" sectional1200="0" bonusindicator="" />
> </race>
> </meeting>
>> If I do this
>>> nomID = [res.eq(i).attr('horse') for i in range(len(res))]
> horseName = [res.eq(i).attr('horse') for i in range(len(res))]
> print(nomID, horseName)
>> comes out correctly
>> In [7]: 171115 Vergara
>> Since I will be taking another 10 attributes out of nominmation category an efficient way that ensures data integrity would be valued.
>> Thanks
>> Sayth

Also if I create it as an iterable only the first list in the list is printed.
group = [nomID, horseName]
iterthis = iter(group)
for items in iterthis:
 print(items)
In [10]: ['Vergara', 'Heavens Above', 'Sweet Fire', 'Alegria', 'Piamimi', 'Blendwell', 'Adorabeel (NZ)', 'Mary Lou', 'Skyline Blush', 'All Cerise', 'Gust of Wind (NZ)']
['Vergara', 'Heavens Above', 'Sweet Fire', 'Alegria', 'Piamimi', 'Blendwell', 'Adorabeel (NZ)', 'Mary Lou', 'Skyline Blush', 'All Cerise', 'Gust of Wind (NZ)']
Sayth


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /