私はあなたがxx['moneyline']
からmd
辞書を保存するために、リストを使用することをお勧めし、保存
data=json.loads(res.text)
dlist=[]
for item in data['leagues'][0]['events']:
for xx in item['periods']:
if 'moneyline' in xx.keys():
d=xx['moneyline']
dlist.append(d)
print(dlist)
ありがとう:
私はに私のコードを変更しますこの辞書のキーを別の辞書に入れて、すべての値を保存します辞書のmd
です。各キーについて、リストは元のmd
辞書に値を格納します。
{'home': [116.0, -369.0, 109.0, 110.0, -240.0], 'away': [303.0, 1062.0, 337.0, 297.0, 731.0], 'draw': [223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 223.0, 577.0, 217.0, 244.0, 415.0]}
ステップ1:
data=json.loads(res.text)
list_dictionary = [] #Initialise an empty list to store the dictionaries
for item in data['leagues'][0]['events']:
for xx in item['periods']:
if 'moneyline' in xx.keys():
md=xx['moneyline']
list_dictionary.append(md). #Append dictionary item to the list
ステップ2:結果はこのようなものになるだろうmd
辞書のキーについての情報を取得し、空の辞書のキーとしてこれらを格納します。空の辞書内のすべてのキーの空のリストを初期化します。
dictionary={}
for key in md.keys():
dictionary.update({key:[]})
ステップ3:ステップ1で得られたlist_dictionaryを反復処理し、このリスト内の各md
辞書のために、すべての値でdictionary
を更新します。
for dict in list_dictionary:
for key, value in dict():
dictionary[key].append(value)
これは、キーが値のリストに対応する1つの辞書内のすべての情報を取得する方法です。
data=json.loads(res.text)
list_dictionary = [] #Initialise an empty list to store the dictionaries
dictionary={} #Initialise an empty dictionary to store all the retrieved data as `md`
for item in data['leagues'][0]['events']:
for xx in item['periods']:
if 'moneyline' in xx.keys():
md=xx['moneyline']
list_dictionary.append(md). #Append dictionary item to the list
for key in md.keys():
dictionary.update({key:[]})
#Store all the `md` values in dictionary as list
for dict in list_dictionary:
for key, value in dict.iteritems():
dictionary[key].append(value)
インデントなしでPythonコードを投稿しないでください。インデントはコードの意味に影響します。 – khelwood
質問にあるコードを修正してください。 – WhatsThePoint
本当にすみません。 –