计算两个列表中的匹配数,并给出拆分原始lis的条件

2024-03-28 23:33:46 发布

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

我有一个浮动列表,其中一些隐藏的“级别”信息以浮动的比例编码,我可以将浮动的“级别”拆分为:

import math
import numpy as np

all_scores = [1.0369411057174144e+22, 2.7997409854370188e+23, 1.296176382146768e+23,
6.7401171871631936e+22, 6.7401171871631936e+22, 2.022035156148958e+24, 8.65845823274041e+23,
1.6435516525621017e+24, 2.307193960221247e+24, 1.285806971089594e+24, 9603539.08653573,
17489013.841076534, 11806185.6660164, 16057293.564414097, 8546268.728385007, 53788629.47091801,
31828243.07349571, 51740168.15200098, 53788629.47091801, 22334836.315934014,
4354.0, 7474.0, 4354.0, 4030.0, 6859.0, 8635.0, 7474.0, 8635.0, 9623.0, 8479.0]

easy, med, hard = [], [], []

for i in all_scores:
    if i > math.exp(50):
        easy.append(i)
    elif i > math.exp(10):
        med.append(i)
    else:
        hard.append(i)

print ([easy, med, hard])

[出来]:

^{pr2}$

我还有另一个与all_scores列表相对应的列表:

input_scores = [0.0, 2.7997409854370188e+23, 0.0, 6.7401171871631936e+22, 0.0, 0.0, 8.6584582327404103e+23, 0.0, 2.3071939602212471e+24, 0.0, 0.0, 17489013.841076534, 11806185.6660164, 0.0, 8546268.728385007, 0.0, 31828243.073495708, 51740168.152000979, 0.0, 22334836.315934014, 4354.0, 7474.0, 4354.0, 4030.0, 0.0, 8635.0, 0.0, 0.0, 0.0, 8479.0]

我需要检查有多少简单的、中等的和困难的匹配所有的分数,我可以这样做来得到布尔值,在平坦的all_scores列表上是否有匹配:

matches = [i == j for i, j in zip(input_scores, all_scores)]
print ([i == j for i, j in zip(input_scores, all_scores)])

[出来]:

[False, True, False, True, False, False, True, False, True, False, False, True, True, False, True, False, True, True, False, True, True, True, True, True, False, True, False, False, False, True]

有没有办法知道比赛中有多少简单/中等/困难,以及每个级别的比赛总数?

我试过了,效果很好:

matches = [int(i == j) for i, j in zip(input_scores, all_scores)]

print(sum(matches[:len(easy)]) , len(easy), sum(np.array(easy) * matches[:len(easy)]) )
print(sum(matches[len(easy):len(easy)+len(med)]), len(med), sum(np.array(med) * matches[len(easy):len(easy)+len(med)]) )
print (sum(matches[len(easy)+len(med):]) , len(hard), sum(np.array(hard) * matches[len(easy)+len(med):]) )

[出来]:

4 10 3.52041505391e+24
6 10 143744715.777
6 10 37326.0

但是必须有一种不那么冗长的方法来实现相同的输出。在


Tags: infalsetrue列表forleneasynp
3条回答

您可以使用dict

k = ('easy', 'meduim', 'hard')    
param = dict.fromkeys(k,0) ; outlist = []
for index,i in enumerate(range(0, len(matches), 10)):
    count = {k[index]:sum(matches[i:i + 10])}
    outlist.append(count)

print(outlist)
[{'easy': 4}, {'meduim': 6}, {'hard': 6}]

听上去像是一份。。。Counter!

如果您还没有遇到它,Counter类似于dict,但是没有用新值替换.update()等方法中的旧值,而是将它们添加到它们上面。所以:

from collections import Counter

counter = Counter({'a': 2})
counter.update({'a': 3})
counter['a']
> 5

因此,您可以使用以下代码获得上述结果:

^{pr2}$

这里是一个numpy解决方案,它使用digitize来创建类别,并使用bincount来计算和计算匹配项。作为一个免费的奖励,这些统计数据也是为剩余的人创建的。在

categories = 'hard', 'med', 'easy'

# get group membership by splitting at e^10 and e^50
# the 'right' keyword tells digitize to include right boundaries
cat_map = np.digitize(all_scores, np.exp((10, 50)), right=True)
# cat_map has a zero in all the 'hard' places of all_scores
# a one in the 'med' places and a two in the 'easy' places

# add a fourth group to mark all non-matches
# we have to force at least one np.array for element-by-element
# comparison to work
cat_map[np.asanyarray(all_scores) != input_scores] = 3

# count
numbers = np.bincount(cat_map)
# count again, this time using all_scores as weights
sums = np.bincount(cat_map, all_scores)

# print
for c, n, s in zip(categories + ('unmatched',), numbers, sums):
    print('{:12}  {:2d}  {:6.4g}'.format(c, n, s))

# output:
#
# hard           6  3.733e+04
# med            6  1.437e+08
# easy           4  3.52e+24
# unmatched     14  5.159e+24

相关问题 更多 >