如何用逗号分割字典的值?
How can I split values of a dictionary by comma?
games = {
"1":"GTA V,FarCry 5",
"2":"Watchdogs II,South Park: The Stick of Truth",
"3":"For Honor,The Forest,South Park: The Fractured but whole"}
for games_value in games.values():
games_value.split(",")
但这没有任何作用...
我想要的:
games = {
"1":"GTA V","FarCry 5"
"2":"Watchdogs II","South Park: The Stick of Truth"
"3":"For Honor","The Forest","South Park: The Fractured but whole"}
提前致谢!
您已接近解决方案,请查看此代码。
games = {
"1":"GTA V,FarCry 5",
"2":"Watchdogs II,South Park: The Stick of Truth",
"3":"For Honor,The Forest,South Park: The Fractured but whole"
}
for games_key in games:
games[games_key] = games[games_key].split(",")
print(games)
#OUTPUT: {'1': ['GTA V', 'FarCry 5'], '2': ['Watchdogs II', 'South Park: The Stick of Truth'], '3': ['For Honor', 'The Forest', 'South Park: The Fractured but whole']}
逻辑:
- 迭代字典键
- 在字典键处写入拆分后的值
games = {
"1":"GTA V,FarCry 5",
"2":"Watchdogs II,South Park: The Stick of Truth",
"3":"For Honor,The Forest,South Park: The Fractured but whole"}
for games_value in games.values():
games_value.split(",")
但这没有任何作用...
我想要的:
games = {
"1":"GTA V","FarCry 5"
"2":"Watchdogs II","South Park: The Stick of Truth"
"3":"For Honor","The Forest","South Park: The Fractured but whole"}
提前致谢!
您已接近解决方案,请查看此代码。
games = {
"1":"GTA V,FarCry 5",
"2":"Watchdogs II,South Park: The Stick of Truth",
"3":"For Honor,The Forest,South Park: The Fractured but whole"
}
for games_key in games:
games[games_key] = games[games_key].split(",")
print(games)
#OUTPUT: {'1': ['GTA V', 'FarCry 5'], '2': ['Watchdogs II', 'South Park: The Stick of Truth'], '3': ['For Honor', 'The Forest', 'South Park: The Fractured but whole']}
逻辑:
- 迭代字典键
- 在字典键处写入拆分后的值