如何用逗号分割字典的值?

问题描述

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']}

逻辑:

  • 遍历字典键
  • 在字典键上写分割后的值

看看dictionary reference