检查单个列表中最常见的一个列表的值(Check most common occurrence of a value of one list in a separate list)

编程入门 行业动态 更新时间:2024-10-25 00:26:35
检查单个列表中最常见的一个列表的值(Check most common occurrence of a value of one list in a separate list)

我有一个数字列表。

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)

更多推荐

本文发布于:2023-08-06 04:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444778.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最常见   列表   列表中   Check   separate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!