def mode(list):
list = sorted(list)
dict = {}
'''iterates through list and gives key value pair
key being the instance in the list, value being
number of times that instance appears in the list'''
for i in list:
#n will be the indicator of the frequency i shows up
n = 0
for e in list:
if e == i:
#n increases by one every time i shows up in the list again
n+=1
dict.update({i:n})
'''initial list of max key, value pair. Will update if another key
shares same max value'''
new_dict = {max(dict, key=dict.get):dict[max(dict, key=dict.get)]}
for key, value in dict.iteritems():
'''if value of instance == value of key, value pair in new_dict, it
shares the max value so add to new dictionary''''
if value == new_dict.get(new_dict.keys()[0]):
new_dict.update({key:value})
return new_dict
编辑
我删除了所有内容。使用这两种新方法:
FindPeaks()方法将使用带有Y值的列表作为参数,并且不会对其进行修改,它还返回一个二维列表,其中该列表的第一个索引是峰值列表,第二个索引在“list”中列出它们的索引。之后,peaksList[0]、peaksList1和所需的最大峰值数作为参数传递给FindMax()方法,返回一个2D列表。此新列表在索引“0”中有一个包含最大峰值的列表(按降序排列),在peaksList的索引“1”处,您可以在列表“list”找到它们,这是您的专用列表。在
在下面的注释“Test”下,您可以找到我运行的所有测试。正如您所看到的,这个方法不能检测平顶,但是您可以将整个列表作为参数传递给FindMax()方法,以及上面描述的所有参数,您将得到平坦峰值中的所有值,它看起来像一个相同值的序列([…',100100100'…])。但是FindPeaks()方法在查找所有其他峰值(不是我所描述的平坦峰值)时将非常有用,因此,如果您有一个平坦的峰值,此方法将不会返回它。在
输出命令行窗口
我希望你觉得这个对你有用。在
我在制作一个stats库时遇到了同样的问题,你可以在github上找到这个库,但是为了回答你的问题,请制作一个以X值为键,Y值为值的字典。使用以下方法查找具有最大值的键:
您可以使用它作为字典的索引来获取该键的值。您要做的是初始化一个新字典,以防其他键共享相同的最大值:
^{pr2}$最后,按键、值对迭代字典,用任何键、值对更新新字典,其中值等于初始max key,value对的值:
相关问题 更多 >
编程相关推荐