给出一个分数列表:
scores = [(1,"abc")]
#newdict = { }
我需要返回一个包含分数字母的字典,以便这个assert
语句返回True
:
for score, letters in scores:
for letter in letters:
assert score == new_dict.get(letter)
答案 0 :(得分:1)
这样做:
>>> scores = [(1,"abc")]
>>> new_dict = dict()
>>> for tup in scores:
... for key in tup[1]:
... new_dict.setdefault(key,tup[0])
>>> print(new_dict)
{'a': 1, 'c': 1, 'b': 1}