从列表中选择较少的频繁元素
我将感谢您解决这个问题的一些帮助。我有一个包含多个元素的列表,我想选择以最低频率的DE元素。如果多个元素的频率最低,我必须将它们全部归还。
例如: List1 = [1,2,2,2,3] 我必须返回1和3 我尝试使用min(list1,key = list1.count)),但此仅返回1 我正在使用Python 3.10
编辑:不允许使用导入
I would appreciate some help with this problem. I have a list with multiple elements and I want to select de element with the lowest frequency. If multiple elements have the lowest frequency, I have to return them all.
For example:
list1 = [1,2,2,2,3]
I have to return 1 and 3
I tried using min(list1, key = list1.count)) but this return 1 only
I am using Python 3.10
Edit: I am not allowed to use import
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为可以通过更多的Google研究来回答这个问题。但是,让我帮助您一些理解。
如果您确实需要算法端的任何帮助,则有多种方法,例如使用hashmap在O(n)时间和空间中找到元素。您还可以对数组进行排序以在o(nlgn)时间但在恒定空间中执行操作。
如果您想找到一些Python Oneliner,那么我会说不要说,因为大多数这些衬里都在欺骗。它们往往很干净,但在后台花费时间。
在您的情况下,
max
或min
旨在获取单个元素。因此,您只会得到一个结果。但是,您可以基于以下代码编辑1:不允许使用导入的意思是什么意思。这是某种面试/作业问题吗?您不应该总共要求提供家庭作业解决方案。
I think this question can be answered with more google research by yourself. But let me help you in some understanding.
If you really need any help from the Algorithm side, then there are multiple approaches like using a Hashmap to find the elements in O(n) time and space. You can also sort the array to perform the operation in O(nlgn) time but in constant space.
If you are trying to find some python oneliner, then I would say don't because most these one liners are deceiving. They tend to very clean but cost time in the background.
In your case
max
ormin
is designed to get a single element. Hence you get only one result. However you can build upon the following codeEdit 1: What do you mean that you are not allowed to use import. Is it some kind of interview/homework question? You shouldn't ask for homework solutions in total at SO.