[英]Dictionary of lists and list of tuples comparison
所以我试图让我的列表字典与我的元组列表相匹配。 (希望这是有道理的)。 我有一本以列表为值的字典,我的值是每本书的个人分数,例如: bob 上的值 5 等于书单中的第一本书,:
d = {'bob':[5,0,0,1,3], 'toms':[3,0,5,3,0], 'sammy': [0,0,0,1,0], 'ted':[5,0,0,6,3]}
和元组列表:
books=[('Douglas Adams', "The Hitchhiker"), ('Richard Adams', 'Watership'), ('Mitch Albom', 'The Five People'), ('Laurie Anderson', 'Speak'), ('Maya Angelou', 'Caged Bird Sings')]
所以我想要的是能够说出当某人的值为 3 或 6 时我可以把他们拉出来说出哪些书有这些评级。 谢谢!
编辑:我想要 output 某种字典,它会说:
selectScores = {bob: ('Maya Angelou', 'Caged Bird Sings'), toms: (Maya Angelou', 'Caged Bird Sings', Laurie Anderson', 'Speak')}
每个人等等
类似的东西我希望成为 output。
你可以尝试这样的事情。 基本上枚举字典值并使用它的索引来访问书籍数组。
d = {'bob':[5,0,0,1,3], 'toms':[3,0,5,3,0], 'sammy': [0,0,0,1,0], 'ted':[5,0,0,6,3]}
books=[('Douglas Adams', "The Hitchhiker"), ('Richard Adams', 'Watership'), ('Mitch Albom', 'The Five People'), ('Laurie Anderson', 'Speak'), ('Maya Angelou', 'Caged Bird Sings')]
select_scores = {}
for key, books_scores in d.items():
for i, score in enumerate(books_scores):
if score == 3 or score == 6:
if key not in select_scores:
select_scores[key] = []
select_scores[key].append(books[i])
print(select_scores)
Output:
{'bob': [('Maya Angelou', 'Caged Bird Sings')], 'toms': [('Douglas Adams', 'The Hitchhiker'), ('Laurie Anderson', 'Speak')], 'ted': [('Laurie Anderson', 'Speak'), ('Maya Angelou', 'Caged Bird Sings')]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.