假设我有一本如下所示的词典:
dict1 = {band1Review: ["good rhythm", "cool performance"], band2Review: ["fast tempo", "too loud"]}
band1Review = ["Not good", "terrible"]
band3Review = ["best band ever"]
如果我知道我有这两个新的评论,有没有一种方法可以让我现有的字典看起来像下面这样?你知道吗
dict1 = {band1Review: ["good rhythm", "cool performance", "Not good", "terrible"], band2Review: ["fast tempo", "too loud"], band3Review: ["best band ever"]}
我也希望这样做有效率,没有乏味的循环,可能会减慢我的程序。有什么建议吗?你知道吗
字典中存储的列表是可变的,可以就地更新。例如,可以使用
append
附加单个项:一切都很好。您声明希望避免“冗长”的循环;我不太清楚这意味着什么,但您当然可以使用
list
类型上的extend
方法避免循环:结果:
相关问题 更多 >
编程相关推荐