[英]Counting the number of times an item occurs in a certain position within an OrderedDict
I have made a series of functions which randomly generates scores of people based on mean performance and standard deviation. 我做了一系列函数,这些函数根据平均性能和标准偏差随机生成人员分数。 I have written a line of code which takes the output from the previous functions and outputs the names of the people in the order of their score:
我编写了一行代码,该代码采用以前功能的输出,并按照分数的顺序输出人的姓名:
sortedlistofnames = [(value) for index, value in enumerate(odsailorscores)]
odsailorsscores being the output containing both the names of the people along with their scores already ordered. odsailorsscores是输出,其中包含人员的姓名以及他们已订购的分数。
The output look like this: 输出如下所示:
['Bob', 'Alice', 'Clare', 'Dennis', 'Eva']
What I need to do is repeat this 6 times and for each set of random values that comes in instead of just outputting the list of names 6 times I need have each position in the list numbered and added to another dictionary. 我需要做的是重复这6次,并为出现的每组随机值而不是仅输出名称列表6次,我需要为列表中的每个位置编号并添加到另一个字典中。
For example the entries would end up looking like so: 例如,条目最终看起来像这样:
[('Alice', [3, 2, 2, 2, 1, 2]), ('Eva', [4, 4, 5, 4, 5, 5]), ('Clare', [1, 5, 3, 3, 3, 1]), ('Dennis', [5, 3, 4, 5, 4, 4]), ('Bob', [2, 1, 1, 1, 2, 3])]
Which would then be put back into one of my previous functions to end up as just a list of ordered names depending on their overall performance over the 6 races. 然后将其放回我以前的功能之一中,最终取决于在6场比赛中的整体表现而成为一个有序名称列表。
If it helps the function to generate the random scores is: 如果它有助于函数生成随机分数,则为:
def scoreGen(input_dict):
generatedResults = []
for key in input_dict:
x, y = input_dict[key]
random_number = gauss(x, math.sqrt(y))
generatedResults.append((key, random_number))
generatedResults = OrderedDict(sorted(generatedResults, key=lambda t: t[1], reverse=True))
return generatedResults
and the output looks like this: 输出看起来像这样:
OrderedDict([('Clare', 103.77155669358106), ('Bob', 100.7887661842925), ('Alice', 100.0), ('Eva', 91.8316903347015), ('Dennis', 90.0)])
EDIT: 编辑:
There seems to be some confusion over what I'm asking which I realise is because I've written this terribly. 我所问的问题似乎有些困惑,因为我写得如此糟糕。
Basically I need a function that can take an INPUT which will look something like this: 基本上我需要一个可以接受INPUT的函数,该函数看起来像这样:
OrderedDict([('Clare', 103.77155669358106), ('Bob', 100.7887661842925), ('Alice', 100.0), ('Eva', 91.8316903347015), ('Dennis', 90.0)])
This input is from another function which takes a dictionary which has the names of each racer and by each one has their mean performance and their standard deviation (this function is shown above), the input here is once such example of a randomly generated score for each racer using these values. 该输入来自另一个函数,该函数带有一个字典,该字典具有每个赛车手的名称,并且每个人都有其平均表现和标准偏差(此函数如上所示),这里的输入曾经是随机产生的得分示例每个赛车手都使用这些值。
The function must be able to run 6 times and make a dictionary/list which looks like this: 该函数必须能够运行6次并创建如下的字典/列表:
[('Alice', [3, 2, 2, 2, 1, 2]), ('Eva', [4, 4, 5, 4, 5, 5]), ('Clare', [1, 5, 3, 3, 3, 1]), ('Dennis', [5, 3, 4, 5, 4, 4]), ('Bob', [2, 1, 1, 1, 2, 3])]
So that this can be entered back into a function (which I have already written) which gives the overall OUTPUT of this: 这样就可以将其重新输入到一个函数中(我已经写过了),该函数给出了该函数的整体输出:
['Bob', 'Alice', 'Clare', 'Dennis', 'Eva']
Which is their positions based on their score over the 6 races. 这是根据他们在6场比赛中的得分而得出的排名。
If I understand correctly, you're starting with a dictionary mapping from racer names to mean and variances of running speed. 如果我理解正确,那么您将开始使用从赛车手名称到运行速度的均值和方差的字典映射。 You want to simulate the results of several races and generate a dictionary mapping from name to a list of race placement results (eg
[1,2,1]
means that person was the fastest in two races and the second fastest in another race). 您想模拟几个种族的结果,并生成一个从名称到种族放置结果列表的字典映射(例如
[1,2,1]
表示该人在两个种族中是最快的,而在另一个种族中是第二个最快的)。
It should be easy to build the final output dictionary you want. 构建所需的最终输出字典应该很容易。 It's entirely unnecessary to turn the ordered dictionary into a list of its keys, as you're doing in your first code block.
就像在第一个代码块中所做的那样,完全没有必要将有序字典变成其键列表。 Instead, you can directly use the
index
you're getting from enumerate
as you iterate over it: 相反,您可以在遍历
index
直接使用从enumerate
获取的index
:
result_dict = collections.defaultdict(list)
for _ in range(number_of_races_to_simulate):
race_result = scoreGen(input_dict)
for place, racer in enumerate(race_result, start=1):
result_dict[racer].append(place)
The result will look much like your desired output, only as a defaultdict
rather than a list of 2-tuples (but you could get the tuples if you want, by using the dictionary's items
method). 结果看起来很像您想要的输出,只是作为
defaultdict
而不是2元组的列表(但是如果需要,您可以使用字典的items
方法获得元组)。
Note that if you don't need to the running speed values from your simulated races anywhere else in your code, you could make scoreGen
return a simple ordered list of racers, rather than a more complicated OrderedDict
with its useless values (the code above won't need to change at all): 请注意,如果您不需要代码中其他任何地方的模拟竞赛的运行速度值,则可以使
scoreGen
返回一个简单的赛车手排序列表,而不是使用其无用值的更复杂的OrderedDict
(上面的代码赢了完全不需要更改):
def scoreGen(input_dict):
def keyfunc(name):
x, y = input_dict[name] # these variables should really be given better names!
return gauss(x, math.sqrt(y))
return sorted(input_dict, key=keyfunc, reverse=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.