Python序数秩
早上好,我有一张桌子,我正试图返回位置(等级) 学生的分数。我到处都找遍了,没有比这更好的了。我以为我可以通过分类来完成,但事实并非如此。这是表格:Python序数秩,python,python-3.x,python-2.7,sorting,bubble-sort,Python,Python 3.x,Python 2.7,Sorting,Bubble Sort,早上好,我有一张桌子,我正试图返回位置(等级) 学生的分数。我到处都找遍了,没有比这更好的了。我以为我可以通过分类来完成,但事实并非如此。这是表格: Name Score Position David 89 3rd James 56 5th Matt 72 4th John 91 1st Iva 56 5th Anna 91
Name Score Position
David 89 3rd
James 56 5th
Matt 72 4th
John 91 1st
Iva 56 5th
Anna 91 1st
我试着写这段代码,但它没有考虑到重复
score = [89, 56, 72, 91, 56,91]
order = sorted(list(set(score)), reverse=True)
position = []
for n in score:
position.append(order.index(n) + 1)
print(position)
这不是最漂亮的代码,但我认为它满足了您的要求:
from collections import Counter
score = [89, 56, 72, 91, 56, 91]
score_set = sorted(list(set(score)), reverse=True) # unique score
dico = dict((v, i + 1) for i, v in enumerate(score_set)) # position of unique score
count = Counter(score) # occurence of each score
total = 1 # indice to get the current position in the score
# update the dico by counting the number of duplicate.
for k, v in sorted(dico.items(), key=lambda x: x[1]): # sort the dico by the value (rank without duplicate)
count_ = count[k]
dico[k] = total
total += count_ # take into account potential duplicate
position = [dico[e] for e in score] # apply the dict to our original list
print(position) # et voilà
这不是最漂亮的代码,但我认为它满足了您的要求:
from collections import Counter
score = [89, 56, 72, 91, 56, 91]
score_set = sorted(list(set(score)), reverse=True) # unique score
dico = dict((v, i + 1) for i, v in enumerate(score_set)) # position of unique score
count = Counter(score) # occurence of each score
total = 1 # indice to get the current position in the score
# update the dico by counting the number of duplicate.
for k, v in sorted(dico.items(), key=lambda x: x[1]): # sort the dico by the value (rank without duplicate)
count_ = count[k]
dico[k] = total
total += count_ # take into account potential duplicate
position = [dico[e] for e in score] # apply the dict to our original list
print(position) # et voilà
如果您愿意使用外部库,我只能推荐:
如果您愿意使用外部库,我只能推荐:
如果您正在寻找不使用任何库的简单解决方案:
score = [89, 56, 72, 91, 56, 91]
sorted_score = sorted(score, reverse=True)
position = [1]
pos = 1
for i in range(1, len(score)):
if sorted_score[i] < sorted_score[i-1]:
pos = i+1
position.append(pos)
print(position)
如果您正在寻找不使用任何库的简单解决方案:
score = [89, 56, 72, 91, 56, 91]
sorted_score = sorted(score, reverse=True)
position = [1]
pos = 1
for i in range(1, len(score)):
if sorted_score[i] < sorted_score[i-1]:
pos = i+1
position.append(pos)
print(position)
您可以使用
defaultdict
在原始列表中存储分数和索引,以便处理重复项:
from collections import defaultdict
scores_dict = defaultdict(list)
for i, s in enumerate(scores):
scores_dict[s].append(i)
positions = [None] * len(scores)
rank = 1
for s in sorted(scores_dict, reverse=True):
indices = scores_dict[s]
for i in indices:
positions[i] = rank
rank += len(indices)
您可以使用
defaultdict
在原始列表中存储分数和索引,以便处理重复项:
from collections import defaultdict
scores_dict = defaultdict(list)
for i, s in enumerate(scores):
scores_dict[s].append(i)
positions = [None] * len(scores)
rank = 1
for s in sorted(scores_dict, reverse=True):
indices = scores_dict[s]
for i in indices:
positions[i] = rank
rank += len(indices)
你期望的结果是什么?你有没有试过打印
顺序
,看看在循环之前会得到什么?打印顺序会打印排序后的列表,但我只想要位置(排名)。所以你想要的是[1,3,2,0,3,0]
?更可能的是[2,4,3,0,4,0]
给定问题中的位置。如果两个人并列第一个位置,下一个人是第二位还是第三位?你期望的结果是什么?你有没有试过打印顺序
,看看在循环之前会得到什么?打印顺序会打印排序后的列表,但我只想要位置(排名)。所以你想要的是[1,3,2,0,3,0]
?更可能的是[2,4,3,0,4,0]
给定问题中的位置。如果两个人并列第一个位置,下一个人是第二位还是第三位?虽然不是最漂亮的代码,但它帮我完成了任务。哈哈哈。谢谢你,这不是最漂亮的代码,但它为我完成了任务。哈哈哈。谢谢,我以为python没有用于排名的库。我从你那里学到了一些新东西。我要读一读,多亏了你,我认为python没有用于排名的库。我从你那里学到了一些新东西。我将读到这篇文章,感谢你给出的所有答案,你的答案是我目前真正理解的。对我来说非常简单和不言自明。谢谢你给出的所有答案,你的答案是我目前真正理解的。对我来说非常简单和不言自明。谢谢你,我刚刚从你提供的链接中阅读了关于排名类型的内容。我现在知道了顺序排名和标准竞争排名(1224排名)之间的区别。谢谢你,也谢谢你的代码。我刚刚从你提供的链接中读完关于排名类型的文章。我现在知道了顺序排名和标准竞争排名(1224排名)之间的区别。谢谢你,也谢谢你的代码。