我有一个复杂的字典,看起来像这样。
{"Div 4 A Herrar": {
"0": {"convertedTime": "86", "awayScore": "0", "homeTeam": "SKIF Semberija", "homeScore": "0", "awayTeam": "Floda BoIF", "time": "00:00", "events": []},
"1": {"convertedTime": "40", "awayScore": "0", "homeTeam": "Partille IF FK ", "homeScore": "0", "awayTeam": "Kode IF", "time": "01:00", "events": []},
"2": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Kongah\u00e4lla", "homeScore": "0", "awayTeam": "Romelanda UF", "time": "02:00", "events": []},
"3": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Virgo", "homeScore": "0", "awayTeam": "United Africa FC", "time": "03:00", "events": []},
"4": {"convertedTime": "", "awayScore": "0", "homeTeam": "Hisingsbacka FC", "homeScore": "0", "awayTeam": "Guldhedens IK", "time": "04:00", "events": []},
"5": {"convertedTime": "", "awayScore": "0", "homeTeam": "IF Warta", "homeScore": "0", "awayTeam": "Bergums IF", "time": "05:00", "events": []}
}
"Div 5 A Herrar": {
"0": {"convertedTime": "86", "awayScore": "0", "homeTeam": "SKIF Semberija", "homeScore": "0", "awayTeam": "Floda BoIF", "time": "00:00", "events": []},
"1": {"convertedTime": "40", "awayScore": "0", "homeTeam": "Partille IF FK ", "homeScore": "0", "awayTeam": "Kode IF", "time": "01:00", "events": []},
"2": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Kongah\u00e4lla", "homeScore": "0", "awayTeam": "Romelanda UF", "time": "02:00", "events": []},
"3": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Virgo", "homeScore": "0", "awayTeam": "United Africa FC", "time": "03:00", "events": []},
"4": {"convertedTime": "", "awayScore": "0", "homeTeam": "Hisingsbacka FC", "homeScore": "0", "awayTeam": "Guldhedens IK", "time": "04:00", "events": []},
"5": {"convertedTime": "", "awayScore": "0", "homeTeam": "IF Warta", "homeScore": "0", "awayTeam": "Bergums IF", "time": "05:00", "events": []}
}
}
还有一些项目,但由于您获得了字典的结构,因此它不相关。
现在我需要做的是遍历此词典中的所有时间键,因为我需要更改它们的值。这意味着它必须迭代来自“Div 4 A Herrar”的6个时间键/对值和来自“Div 5 A Herrar”中的6个。我怎么能完成那些我完全迷失的事情
答案 0 :(得分:1)
要获得时间值:
for key1 in dict:
for key2 in dict[key1]:
dict[key1][key2]['time'] = 'newTime'
答案 1 :(得分:0)
如果您呈现的结构维护,您可以使用这两种方法之一。否则,你必须做一个递归函数。
dict_of_herrars = {
#...
}
# herrar = "Div 4 A Herrar", etc
for herrar, herrar_dict in dict_of_herrars.items():
# game = "0","1",etc
for game, game_dict in herrar_dict.items():
game_dict["time"]="13:37"
def iter_time(dict_of_herrars):
# herrar = "Div 4 A Herrar", etc
for herrar, herrar_dict in dict_of_herrars.items():
# game = "0","1",etc
for game, game_dict in herrar_dict.items():
def change_time(new_time,game_dict=game_dict):
game_dict["time"]=newtime
yield change_time
for change_time in iter_time(dict_of_herrars):
change_time("13:37")
答案 2 :(得分:0)
d = {"Div 4 A Herrar" : {
"0": {"convertedTime": "86", "awayScore": "0", "homeTeam": "SKIF Semberija", "homeScore": "0", "awayTeam": "Floda BoIF", "time": "00:00", "events": []},
"1": {"convertedTime": "40", "awayScore": "0", "homeTeam": "Partille IF FK ", "homeScore": "0", "awayTeam": "Kode IF", "time": "01:00", "events": []},
"2": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Kongah\\u00e4lla", "homeScore": "0", "awayTeam": "Romelanda UF", "time": "02:00", "events": []},
"3": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Virgo", "homeScore": "0", "awayTeam": "United Africa FC", "time": "03:00", "events": []},
"4": {"convertedTime": "", "awayScore": "0", "homeTeam": "Hisingsbacka FC", "homeScore": "0", "awayTeam": "Guldhedens IK", "time": "04:00", "events": []},
"5": {"convertedTime": "", "awayScore": "0", "homeTeam": "IF Warta", "homeScore": "0", "awayTeam": "Bergums IF", "time": "05:00", "events": []}
},
"Div 5 A Herrar" : {
"0": {"convertedTime": "86", "awayScore": "0", "homeTeam": "SKIF Semberija", "homeScore": "0", "awayTeam": "Floda BoIF", "time": "00:00", "events": []},
"1": {"convertedTime": "40", "awayScore": "0", "homeTeam": "Partille IF FK ", "homeScore": "0", "awayTeam": "Kode IF", "time": "01:00", "events": []},
"2": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Kongah\\u00e4lla", "homeScore": "0", "awayTeam": "Romelanda UF", "time": "02:00", "events": []},
"3": {"convertedTime": "", "awayScore": "0", "homeTeam": "IK Virgo", "homeScore": "0", "awayTeam": "United Africa FC", "time": "03:00", "events": []},
"4": {"convertedTime": "", "awayScore": "0", "homeTeam": "Hisingsbacka FC", "homeScore": "0", "awayTeam": "Guldhedens IK", "time": "04:00", "events": []},
"5": {"convertedTime": "", "awayScore": "0", "homeTeam": "IF Warta", "homeScore": "0", "awayTeam": "Bergums IF", "time": "05:00", "events": []}
}
}
for i in d:
for j in d[i]:
for k in d[i][j]:
if k == "time":
print "Original Time: ", d[i][j][k]
d[i][j][k] = "something"
print "Changed to: ", d[i][j][k]
Original Time: 01:00
Changed to: something
Original Time: 00:00
Changed to: something
Original Time: 03:00
Changed to: something
Original Time: 02:00
Changed to: something
Original Time: 05:00
Changed to: something
Original Time: 04:00
Changed to: something
Original Time: 01:00
Changed to: something
Original Time: 00:00
Changed to: something
Original Time: 03:00
Changed to: something
Original Time: 02:00
Changed to: something
Original Time: 05:00
Changed to: something
Original Time: 04:00
Changed to: something