Python - 如果key小于number且超过一定百分比,则从字典中删除所有键

时间:2016-03-15 21:15:59

标签: python dictionary

我目前可以使用以下方法删除所有低于3.0的键:

tempratings={'Shane': {'127 Hours': 4.5, 'The Revenant': 4.8, 'Panic Room 4.8': 3.7, 'The Spotlight': 3.6, 'Panic Room': 4.8, 'How I Live Now': 4.6, 'Mad Max: Fury Road': 5.0, 'The Martian': 4.8, 'The Hunger Games': 4.5, 'Interstellar': 4.5, 'Dead Poets Society': 5.0, 'The Finest Hours': 4.7, 'Get Hard': 2.0}, 'Aaron': {'Mad Max: Fury Road': 4.0, 'Pacific Rim': 3.0, 'John Wick': 4.0, 'The Blair Witch Project': 5.0, 'Scott Pilgrim Vs The World': 4.0, 'A Talking Cat': 5.0, 'Space Jam': 5.0}, 'Eli': {'The Guardians Of The Galaxy': 3.5, 'The Breakfast Club': 5.0, 'Back To The Future': 4.0, 'E.T.': 5.0, 'Mad Max Beyond Thunderdome': 3.0, 'Mr. Smith Goes To Washington': 4.7, 'Meet Joe Black': 5.0, 'Jurassic Park': 4.0, 'Pulp Fiction': 4.0, 'Mad Max: Fury Road': 5.0, 'The Martian': 4.5, 'Die Hard': 4.0, 'The Dark Knight': 5.0, 'Dead Poets Society': 5.0, 'The Shining': 3.5, 'Inception': 5.0, 'Mad Max': 4.0, '127 Hours': 3.0, 'Rocky': 5.0, 'Blade Runner': 3.5, 'The Wizard Of Oz': 5.0, 'Interstellar': 5.0, 'The Sixth Sense': 3.5, 'Gladiator': 5.0, 'The Lion King': 5.0, 'Toy Story 3': 3.5, 'Good Will Hunting': 5.0, 'The Revenant': 4.5, 'The Matrix': 5.0, 'Full Metal Jacket': 4.5, 'Inglourious Basterds': 5.0, 'Forrest Gump': 5.0, 'Saving Private Ryan': 5.0, 'John Wick': 3.5, 'Up': 3.0, 'Titanic': 5.0, 'Avatar': 5.0, 'Jurassic World': 3.5, 'The Silence Of The Lambs': 4.5, 'Seven Pounds': 5.0, 'Jaws': 4.5, 'The Wolf Of Wall Street': 5.0, 'Alien': 4.5, 'Whiplash': 5.0, 'Finding Nemo': 5.0, 'Mad Max 2: The Road Warrior': 4.5}}

for i in range(len(similarlisthigh)):
        for key in tempratings[similarlisthigh[i]].keys():  # creates a list of all keys
            if tempratings[similarlisthigh[i]][key] < 3.0: #remove ratings below tolerance
                del tempratings[similarlisthigh[i]][key]

这将删除dict tempratings中具有低于3.0的相应值的所有键,但保留3或更高的相同键。

导致(现在没有值低于3.0的键):

{'Shane': {'127 Hours': 4.5, 'The Revenant': 4.8, 'Panic Room 4.8': 3.7, 'The Spotlight': 3.6, 'Panic Room': 4.8, 'How I Live Now': 4.6, 'Mad Max: Fury Road': 5.0, 'The Martian': 4.8, 'The Hunger Games': 4.5, 'Interstellar': 4.5, 'Dead Poets Society': 5.0, 'The Finest Hours': 4.7}, 'Aaron': {'Mad Max: Fury Road': 4.0, 'Pacific Rim': 3.0, 'John Wick': 4.0, 'The Blair Witch Project': 5.0, 'Scott Pilgrim Vs The World': 4.0, 'A Talking Cat': 5.0, 'Space Jam': 5.0}, 'Eli': {'The Guardians Of The Galaxy': 3.5, 'The Breakfast Club': 5.0, 'Back To The Future': 4.0, 'E.T.': 5.0, 'Mad Max Beyond Thunderdome': 3.0, 'Mr. Smith Goes To Washington': 4.7, 'Meet Joe Black': 5.0, 'Jurassic Park': 4.0, 'Pulp Fiction': 4.0, 'Mad Max: Fury Road': 5.0, 'The Martian': 4.5, 'Die Hard': 4.0, 'The Dark Knight': 5.0, 'Dead Poets Society': 5.0, 'The Shining': 3.5, 'Inception': 5.0, 'Mad Max': 4.0, '127 Hours': 3.0, 'Rocky': 5.0, 'Blade Runner': 3.5, 'The Wizard Of Oz': 5.0, 'Interstellar': 5.0, 'The Sixth Sense': 3.5, 'Gladiator': 5.0, 'The Lion King': 5.0, 'Toy Story 3': 3.5, 'Good Will Hunting': 5.0, 'The Revenant': 4.5, 'The Matrix': 5.0, 'Full Metal Jacket': 4.5, 'Inglourious Basterds': 5.0, 'Forrest Gump': 5.0, 'Saving Private Ryan': 5.0, 'John Wick': 3.5, 'Up': 3.0, 'Titanic': 5.0, 'Avatar': 5.0, 'Jurassic World': 3.5, 'The Silence Of The Lambs': 4.5, 'Seven Pounds': 5.0, 'Jaws': 4.5, 'The Wolf Of Wall Street': 5.0, 'Alien': 4.5, 'Whiplash': 5.0, 'Finding Nemo': 5.0, 'Mad Max 2: The Road Warrior': 4.5}}

最好的方法是始终删除值低于3.0 的所有键,但只有删除其他相同的键,无论值如果以下键的等级3.0高于50%?

例如,如果Eli评价泰坦尼克号5.0,Jaycee评级为4.5,Olen评级为1.0,Aaron评级为3.6,它会删除所有“泰坦尼克号”键,只是Olen's因为百分比值小于3.0的键值低于50%。实际上是25%。

但如果Eli评价Alien 2.0,则Jaycee将其评为4.0,Olen评级为1.2,而Aaron评级为2.9,所有“Alien”实例将被删除,因为3.0以下评级的百分比高于50%。 75%的评级都在。

1 个答案:

答案 0 :(得分:2)

我认为你需要经历两次以获得所有分数,然后第二次进行平均分析

list_of_dicts = [
  {'127 Hours': 5.0, 'Panic Room': 5.0, 'The Martian': 5.0, 'Revenant': 4.5, 'Avatar': 5.0, 'Nonstop': 4.0},
  {'127 Hours': 5.0, 'Panic Room': 4.0, 'Mad Max': 2.0},
  {'127 Hours': 1.0, 'Panic Room': 2.0, 'Mad Max': 4.0, 'Panic Room': 2.0}     
]
new_data = {}
for data in list_of_dicts:
    for title,score in data.items():
        new_data.setdefault(title,[]).append(score)
def solve_list(scores):
    len_list = len(scores)
    percents = [float(scores.count(x))/len_list for x in scores]
    final_list = [value for value,pct in zip(scores,percents) if pct > 0.3]
    return numpy.average(final_list)
final_dict = dict((k,solve_lists(v)) for k,v in new_data.items())

虽然我不完全清楚你如何得到你所列出的分数......因为你只是省略了一些评论分数