我有一个数字列表。
somelist = [5.000007,5.00099,5.0000075,5.0000075,5.0000075,5.0000099,5.00099,5.0000080,5.0000081,5.00099,5.0000080,5.0000096,5.0000087,5.008,5.00099,5.00000009]我正在使用以下内容生成3个最低值的唯一列表:
def lowest_three(somelist): lowest_unique = set(somelist) return nsmallest(3, lowest_unique)它产生输出:
[5.00000009, 5.000007, 5.0000075]现在我想要一个单独的函数来告诉我原始列表中最常出现的三个最低值中的哪一个。
所以我希望它告诉我5.0000075是原始列表( somelist )中lowest_three列表中最常见的数字。
我已经尝试了以下但它不起作用(它目前产生的输出为5.00099 ,甚至lowest_three列表中)。
def most_common_lowest(somelist): for x in lowest_three(somelist): return max(set(somelist), key=somelist.count)怎么能实现这个?
I have a list of numbers.
somelist = [5.000007,5.00099,5.0000075,5.0000075,5.0000075,5.0000099,5.00099,5.0000080,5.0000081,5.00099,5.0000080,5.0000096,5.0000087,5.008,5.00099,5.00000009]I’m using the following to produce a unique list of the 3 lowest values:
def lowest_three(somelist): lowest_unique = set(somelist) return nsmallest(3, lowest_unique)It produces the output:
[5.00000009, 5.000007, 5.0000075]Now I want a separate function to tell me which of the three lowest values is the most commonly occuring in the original list.
So I want it to tell me that 5.0000075 is the most common number from the lowest_three list in the original list (somelist).
I’ve tried the following but it’s not working (it’s currently producing an output of 5.00099 which isn’t even in the lowest_three list).
def most_common_lowest(somelist): for x in lowest_three(somelist): return max(set(somelist), key=somelist.count)How can achieve this?
最满意答案
现在我想要一个单独的函数来告诉我原始列表中最常出现的三个最低值中的哪一个。
def most_common_lowest(somelist): for x in lowest_three(somelist): return max(set(somelist), key=somelist.count)
那段代码没有意义。 应该:
def most_common_lowest(somelist): return max(lowest_three(somelist), key=somelist.count)Now I want a separate function to tell me which of the three lowest values is the most commonly occuring in the original list.
def most_common_lowest(somelist): for x in lowest_three(somelist): return max(set(somelist), key=somelist.count)
That code doesn't make sense. Should be:
def most_common_lowest(somelist): return max(lowest_three(somelist), key=somelist.count)更多推荐
发布评论