我想从社区寻求帮助。.我在这里有2个相关列表:
names = ['alan_grant', 'alan_grant', 'alan_grant', 'alan_grant', 'alan_grant', 'claire_dearing', 'claire_dearing', 'claire_dearing', 'claire_dearing', 'claire_dearing', 'ellie_sattler', 'ellie_sattler', 'ellie_sattler', 'ellie_sattler', 'ellie_sattler', 'ian_malcolm', 'ian_malcolm', 'ian_malcolm', 'ian_malcolm', 'ian_malcolm', 'john_hammond', 'john_hammond', 'john_hammond', 'john_hammond', 'john_hammond', 'owen_grady', 'owen_grady', 'owen_grady', 'owen_grady', 'owen_grady']
votes = [True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, True, True, True]
票
列表是面部识别算法匹配的面部识别算法的结果。然后,我将将每个 true
投票链接到相应的名称,并找到最常见的名称是最终的“获胜者”。
我尝试了两种方法:
characters = {}
for name, vote in list(zip(names, votes)):
if vote == True:
characters[name] = characters.get(name, 0) + 1
#print(characters)
print(max(characters, key=characters.get))
输出是'Owen_grady'
from collections import Counter
characters = [name for name, vote in list(zip(names, votes)) if vote == True]
#print(characters)
print(Counter(characters).most_common()[0][0])
输出也为“ Owen_grady”。哪种方式更有效:字典?还是通过计数器列出理解?
我的最终问题是:是否有另一种方法(最有效的)获得结果?我希望输出只是“ owen_grady”
I would like to seek help from the community.. I have 2 related lists here:
names = ['alan_grant', 'alan_grant', 'alan_grant', 'alan_grant', 'alan_grant', 'claire_dearing', 'claire_dearing', 'claire_dearing', 'claire_dearing', 'claire_dearing', 'ellie_sattler', 'ellie_sattler', 'ellie_sattler', 'ellie_sattler', 'ellie_sattler', 'ian_malcolm', 'ian_malcolm', 'ian_malcolm', 'ian_malcolm', 'ian_malcolm', 'john_hammond', 'john_hammond', 'john_hammond', 'john_hammond', 'john_hammond', 'owen_grady', 'owen_grady', 'owen_grady', 'owen_grady', 'owen_grady']
votes = [True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, True, True, True, True]
The votes
list is a result of a facial recognition algorithm matching from the corresponding names
list. Then I shall link each True
vote to the corresponding name, and find the most frequently occurred name to be the final 'winner'.
I have tried 2 ways:
characters = {}
for name, vote in list(zip(names, votes)):
if vote == True:
characters[name] = characters.get(name, 0) + 1
#print(characters)
print(max(characters, key=characters.get))
The output is 'owen_grady'
from collections import Counter
characters = [name for name, vote in list(zip(names, votes)) if vote == True]
#print(characters)
print(Counter(characters).most_common()[0][0])
The output is also 'owen_grady'. Which way is more efficient: Dictionary? or List Comprehension with Counter?
My ultimate question: is there another way (the most efficient) to get the result? I would like the output to be just 'owen_grady'
发布评论
评论(2)
您可以使用
itertools.compress() /a>要过滤所有 false 条目。
计数器
的选项应最有效,只需在n
参数/library/collections.html#collections.counter.most_common“ rel =” nofollow noreferrer“>.most_common()
让它返回单个对。Code:
Upd. I've made some benchmarks对于这种特殊情况,似乎略微优化的第一种方法表明了更好的性能:
You can use
itertools.compress()
to filter all false entries. Option withCounter
should be most efficient, just usen
argument in.most_common()
to let it return a single pair.Code:
Upd. I've made some benchmarks and it seems like for this particular case slightly optimized first method demonstrates better performance:
您还可以尝试使用
Counter
模块解决方案,除了zip()
函数:output:
You can try to stay with the
Counter
module solution in addition to thezip()
function:Output: