Python 列表字典和元组列表比较
所以我试图让我的列表字典与我的元组列表相匹配。(希望这是有道理的)。我有一本以列表为值的字典,我的值是每本书的单独分数,例如:bob上的值5等于图书列表中的第一本书:Python 列表字典和元组列表比较,python,list,dictionary,tuples,Python,List,Dictionary,Tuples,所以我试图让我的列表字典与我的元组列表相匹配。(希望这是有道理的)。我有一本以列表为值的字典,我的值是每本书的单独分数,例如: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', '
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时,我可以把他们拉出来,告诉他们什么书有这样的评级。
谢谢
编辑:
我希望它能输出某种类型的字典,它会说:
selectScores = {bob: ('Maya Angelou', 'Caged Bird Sings'), toms: (Maya Angelou', 'Caged Bird Sings', Laurie Anderson', 'Speak')}
对每个人来说都是如此
像这样的东西我希望是输出。你可以试试这样的东西。基本上枚举字典值并使用其索引访问books数组
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)
输出:
{'bob': [('Maya Angelou', 'Caged Bird Sings')], 'toms': [('Douglas Adams', 'The Hitchhiker'), ('Laurie Anderson', 'Speak')], 'ted': [('Laurie Anderson', 'Speak'), ('Maya Angelou', 'Caged Bird Sings')]}
你的问题是什么?请澄清。如果您正在寻找编写代码的帮助,那么您已经尝试了什么?所以这不是一个代码编写服务,但我们很乐意帮助您编写代码。看看你是否需要更多的提示。你的问题还不完全清楚,也许你可以包括你的预期输出,并根据你自己的研究为你迄今为止尝试过的东西编写代码,以做出一个结论。你能添加一些预期输出吗?我真的不明白你说的“当某人的值为3或6”是什么意思