変数に割り当てる多くの繰り返しコードを減らす関数を作成しようとしています。私はこれを行う場合リスト内の属性の一致を取得する関数
現在、この出力を生成
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)
動作します。
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)
ただし、これを続けるには、このような変数を以下に追加する必要があります。唯一の変更部分は変数名であり、この場合、ATTR(「馬」)内の属性は
horseName = [res.eq(i).attr('horse') for i in range(len(res))]
だから、乾燥して、リストである引数を取る関数を作成するために、論理だろうどこ
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)
属性ええと...あなたは 'inputs'機能を使用しない、それが使用されることはありません引数を取りますか? –
_これを続けていくうちに、私はこれらの多くを作り出す必要があります。私はあなたが何を追加したいのか、どこにどこを置く必要があるのか本当に分かりません。 – AKS
検索する各属性の変数 – sayth