为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕慕森 2023-09-19 14:01:23
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"}
查看完整描述

1 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

您已经接近解决方案了,请查看此代码。


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

逻辑:

  • 迭代字典键

  • 在字典键处写入分割后的值

查看完整回答
反对 回复 2023-09-19
  • 1 回答
  • 0 关注
  • 85 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信