What iterable method should I use for Lists of Lists

Peter Otten __peter__ at web.de
Mon Apr 18 06:23:05 EDT 2016


Sayth Renshaw wrote:

> Think I have a solution of sorts, although my numpy array failed, zip
> worked.
> 
> from pyquery import PyQuery as pq
> import numpy as np
> 
> 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('id') for i in range(len(res))]
> horseName = [res.eq(i).attr('horse') for i in range(len(res))]
> zipped = zip(nomID, horseName)
> 
> # yes = np.array(zipped)
> for items in zipped:
>     print(items)
> 
> In [8]: ('171115', 'Vergara')
> ('187674', 'Heavens Above')
> ('184732', 'Sweet Fire')
> ('181928', 'Alegria')
> ('158914', 'Piamimi')
> ('171408', 'Blendwell')
> ('166836', 'Adorabeel (NZ)')
> ('172933', 'Mary Lou')
> ('182533', 'Skyline Blush')
> ('171801', 'All Cerise')
> ('181079', 'Gust of Wind (NZ)')
> 
> Still interested if there is a better to do this.

I don't know pyquery, so there may still be better ways. This is how far I 
got with dir() and the common Python wisdom "Never use range(len(...))":

>>> import pyquery
>>> d = pyquery.PyQuery(filename="horse.xml")
>>> pairs = [(n.attrib["horse"], n.attrib["id"]) for n in d("nomination")]
>>> import pprint
>>> pprint.pprint(pairs)
[('Vergara', '171115'),
 ('Heavens Above', '187674'),
 ('Sweet Fire', '184732'),
 ('Alegria', '181928'),
 ('Piamimi', '158914'),
 ('Blendwell', '171408'),
 ('Adorabeel (NZ)', '166836'),
 ('Mary Lou', '172933'),
 ('Skyline Blush', '182533'),
 ('All Cerise', '171801'),
 ('Gust of Wind (NZ)', '181079')]

pprint is not really necessary, it just gives more readable output than

>>> pairs
[('Vergara', '171115'), ('Heavens Above', '187674'), ('Sweet Fire', 
'184732'), ('Alegria', '181928'), ('Piamimi', '158914'), ('Blendwell', 
'171408'), ('Adorabeel (NZ)', '166836'), ('Mary Lou', '172933'), ('Skyline 
Blush', '182533'), ('All Cerise', '171801'), ('Gust of Wind (NZ)', 
'181079')]

To extract more than one attribute operator.itemgetter comes in handy:

>>> from operator import itemgetter
>>> get = itemgetter("horse", "id", "saddlecloth")
>>> pprint.pprint([get(n.attrib) for n in d("nomination")])
[('Vergara', '171115', '4'),
 ('Heavens Above', '187674', '2'),
 ('Sweet Fire', '184732', '6'),
 ('Alegria', '181928', '7'),
 ('Piamimi', '158914', '11'),
 ('Blendwell', '171408', '10'),
 ('Adorabeel (NZ)', '166836', '3'),
 ('Mary Lou', '172933', '8'),
 ('Skyline Blush', '182533', '9'),
 ('All Cerise', '171801', '5'),
 ('Gust of Wind (NZ)', '181079', '1')]

If you need to do some post-processing use a helper function:

>>> def extract(n):
...     attrib = n.attrib
...     id = int(attrib["id"])
...     name = attrib["horse"].upper()
...     return id, name
... 
>>> pprint.pprint([extract(n) for n in d("nomination")])
[(171115, 'VERGARA'),
 (187674, 'HEAVENS ABOVE'),
 (184732, 'SWEET FIRE'),
 (181928, 'ALEGRIA'),
 (158914, 'PIAMIMI'),
 (171408, 'BLENDWELL'),
 (166836, 'ADORABEEL (NZ)'),
 (172933, 'MARY LOU'),
 (182533, 'SKYLINE BLUSH'),
 (171801, 'ALL CERISE'),
 (181079, 'GUST OF WIND (NZ)')]





More information about the Python-list mailing list