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

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"}


慕慕森
浏览 86回答 1
1回答

慕无忌1623718

您已经接近解决方案了,请查看此代码。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']}逻辑:迭代字典键在字典键处写入分割后的值
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python