【问题标题】:How to get the least common element in a list?如何获取列表中最不常见的元素?
【发布时间】:2011-01-20 02:58:06
【问题描述】:

要找到最常见的,我知道我可以使用这样的东西:

most_common = collections.Counter(list).most_common(to_find)

但是,我似乎找不到任何可比的东西来寻找最不常见的元素。

能否请我获得有关如何操作的建议。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    most_common 不带任何参数返回所有个条目,从最常见到最少。

    所以要找到最不常见的,只需从另一端开始查看。

    【讨论】:

      【解决方案2】:

      怎么样

      least_common = collections.Counter(array).most_common()[-1]
      

      【讨论】:

      • 这很简单。谢谢:D
      【解决方案3】:

      借用collections.Counter.most_common的出处并酌情取反:

      from operator import itemgetter
      import heapq
      import collections
      def least_common_values(array, to_find=None):
          counter = collections.Counter(array)
          if to_find is None:
              return sorted(counter.items(), key=itemgetter(1), reverse=False)
          return heapq.nsmallest(to_find, counter.items(), key=itemgetter(1))
      
      >>> data = [1,1,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4]
      >>> least_common_values(data, 2)
      [(1, 2), (2, 4)]
      >>> least_common_values([1,1,2,3,3])
      [(2, 1), (1, 2), (3, 2)]
      >>>
      

      【讨论】:

        【解决方案4】:

        抱歉,这个帖子迟到了……发现文档很有帮助: https://docs.python.org/3.7/library/collections.html

        搜索“最少”,您会看到这个表格,它有助于获得比列表中最后一个 (-1) 更多的元素:

        c.most_common()[:-n-1:-1]       # n least common elements
        

        这是一个例子:

        n = 50
        
        word_freq = Count(words)
        least_common = word_freq.most_common()[:-n-1:-1]
        

        【讨论】:

        • 是的,这是一个很好的答案,它是一个最小通用的实现
        【解决方案5】:
        def least_common_values(array, to_find):
            """
            >>> least_common_values([1,1,2,2,2,2,3,3,3,3,3,3,3,4,4,4,4,4,4,4], 2)
            [(1, 2), (2, 4)]
            """
            counts = collections.Counter(array)
            return list(reversed(counts.most_common()[-to_find:]))
        

        【讨论】:

          【解决方案6】:

          只获取最不常见的元素,仅此而已:

          >>> from collections import Counter
          >>> ls = [1, 2, 3, 3, 2, 5, 1, 6, 6]
          >>> Counter(ls).most_common()[-1][0]
          5
          

          【讨论】:

            【解决方案7】:

            我猜你需要这个:

            least_common = collections.Counter(array).most_common()[:-to_find-1:-1]
            

            【讨论】:

              【解决方案8】:

              Iterable 中实现最小值搜索的最简单方法如下:

              Counter(your_iterable).most_common()[-1]
              

              返回一个二维元组,其中包含第一个位置的元素和第二个位置的出现次数。

              【讨论】:

                【解决方案9】:

                我建议如下,

                least_common = collections.Counter(array).most_common()[len(to_find)-10:len(to_find)]
                

                【讨论】:

                  【解决方案10】:

                  可以使用按键功能:

                  >>> data=[1,1,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4,4,4,4]
                  >>> min(data,key=lambda x: data.count(x))
                  1
                  >>> max(data,key=lambda x: data.count(x))
                  4
                  

                  【讨论】:

                    【解决方案11】:

                    基于此对最常见元素的回答:https://stackoverflow.com/a/1518632

                    这是获取列表中最不常见元素的单行代码:

                    def least_common(lst):
                        return min(set(lst), key=lst.count)
                    

                    【讨论】:

                    • 这很聪明,也可以通过将 min() 转换为 max() 来计算相似变体中最常见的值
                    猜你喜欢
                    • 2021-04-10
                    • 1970-01-01
                    • 1970-01-01
                    • 2022-01-18
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2012-11-12
                    相关资源
                    最近更新 更多