Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107263
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: What iterable method should I use for Lists of Lists |
| Date | 2016-04-18 12:23 +0200 |
| Organization | None |
| Message-ID | <mailman.145.1460975002.6324.python-list@python.org> (permalink) |
| References | <67c4c88b-62e9-4275-a761-25e5d7114025@googlegroups.com> <b90ac09a-7ab1-48cd-a966-f66f8f0376fa@googlegroups.com> <0a8c3fd1-ca01-4a62-b2fb-10fd841cde74@googlegroups.com> <nf2cia$pcn$1@ger.gmane.org> |
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)')]
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
What iterable method should I use for Lists of Lists Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-17 19:05 -0700
Re: What iterable method should I use for Lists of Lists Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-17 19:12 -0700
Re: What iterable method should I use for Lists of Lists Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-17 20:13 -0700
Re: What iterable method should I use for Lists of Lists Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-17 21:09 -0700
Re: What iterable method should I use for Lists of Lists Chris Angelico <rosuav@gmail.com> - 2016-04-18 14:40 +1000
Re: What iterable method should I use for Lists of Lists Sayth Renshaw <flebber.crue@gmail.com> - 2016-04-18 00:13 -0700
Re: What iterable method should I use for Lists of Lists Peter Otten <__peter__@web.de> - 2016-04-18 12:23 +0200
csiph-web