繁体   English   中英

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

[英]Python - Remove all keys from dictionary if key is less than number and over certain percentage

我目前可以使用以下命令删除所有低于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]

这将从具有相应值低于3.0的dict tempratings中删除所有键,但tempratings或大于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评估Titanic 5.0,Jaycee评估4.5,Olen评估1.0,Aaron评估3.6,则它不会删除所有“ Titanic”键,只是Olen删除了,因为值低于3.0的键百分比低于50 %。 实际是25%

但是,如果Eli给Alien评分为2.0,Jaycee评分为4.0,Olen评分为1.2,Aaron评分为2.9,则所有“ Alien”实例将被删除,因为低于3.0的评分百分比高于50%。 低于75%的评分。

我认为您将需要经历两次以获取所有分数,然后再次进行平均

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())

尽管我还不太清楚如何获得列出的预期分数...因为您只是省略了一些评价分数

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM