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

2024-09-29 22:01:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这将删除dicttempratings中对应值低于3.0的所有键,但保留3或以上的相同键。在

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

^{pr2}$

如果低于3.0的键的额定值高于50%,那么什么是始终移除所有值低于3.0的键的最佳方法,但只移除其他相同的键而不考虑值?在

例如,如果Eli给泰坦尼克号评分5.0,Jayce评分4.5,Olen评分1.0,Aaron评分3.6,则不会删除所有的“泰坦尼克号”键,只删除Olen键,因为值低于3.0的键的百分比低于50%。实际上是25%。在

但是如果Eli给Alien 2.0的评分,Jayce的评分是4.0,Olen的评分是1.2,Aaron的评分是2.9,那么所有的“Alien”都会被删除,因为3.0以下的评分比例高于50%。75%的评分低于。在


Tags: ofthekey评分maxfuryaaronmad
1条回答
网友
1楼 · 发布于 2024-09-29 22:01:12

我想你需要经过两次,一次得到所有的分数,然后第二次平均

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

虽然我不完全清楚你是如何得到你所期望的分数。。。因为你只是漏掉了一些复习分数

相关问题 更多 >

    热门问题