我有几个字典(Class1 , Class2
),字典的一个元素存储一个列表(Score
),我想将列表的元素放入另一个列表,而不是将列表本身放入另一个列表。
我尝试以下代码
All = []
Class1 = {"name":"A","score":[60,70,80]}
Class2 = {"naem":"B","score":[70,80,90]}
All.append(Class1['score'])
All.append(Class2['score'])
print(All)
但结果是
[[60, 70, 80], [70, 80, 90]]
但我想要的是
[60, 70, 80, 70, 80, 90]
我在下面尝试这个解决方案,但我想知道是否存在更好的解决方案?
All = []
Class1 = {"name":"A","score":[60,70,80]}
Class2 = {"naem":"B","score":[70,80,90]}
Scores1 = Class1['score']
Scores2 = Class2['score']
Scores = Scores1 + Scores2
for score in Scores:
All.append(score)
print(All)
谢谢