I am trying to create a function to reduce what will be a lot of repeated code assigning to variables.
Currently if I do this it works
from pyquery import PyQuery as pq
import pandas as pd
d = pq(filename='20160319RHIL0_edit.xml')
# from nominations
res = d('nomination')
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 = list(zip(nomID, horseName))
frames = pd.DataFrame(zipped)
print(frames)
Producing this output.
In [9]: 0 1
0 171115 Vergara
1 187674 Heavens Above
2 184732 Sweet Fire
3 181928 Alegria
4 158914 Piamimi
5 171408 Blendwell
6 166836 Adorabeel (NZ)
7 172933 Mary Lou
8 182533 Skyline Blush
9 171801 All Cerise
10 181079 Gust of Wind (NZ)
However to keep adding to this I would need to create more variables like this one(below). Where the only changing part is the variable name and the attribute in this case attr('horse')
horseName = [res.eq(i).attr('horse') for i in range(len(res))]
So it would be logical then to DRY and create a function which takes an argument which is a list of attributes
from pyquery import PyQuery as pq
import pandas as pd
d = pq(filename='20160319RHIL0_edit.xml')
# from nominations
res = d('nomination')
aList = []
def inputs(args):
'''function to get elements matching criteria'''
optsList = ['id', 'horse']
for item in res:
for attrs in optsList:
if res.attr(attrs) in item:
aList.append([res.eq(i).attr(attrs) for i in range(len(res))])
zipped = list(zip(aList))
frames = pd.DataFrame(zipped)
print(frames)
attrs = ('id', 'horse', ...)
...
data = [[res.eq(i).attr(x) for x in attrs] for i in range(len(res))]