Hướng dẫn python get most common element in list - python lấy phần tử phổ biến nhất trong danh sách

Với rất nhiều giải pháp được đề xuất, tôi ngạc nhiên khi không ai đề xuất những gì tôi coi là một giải pháp rõ ràng (đối với các yếu tố không thể so sánh nhưng có thể so sánh)-[____ ____ 6] [1]. itertools cung cấp chức năng nhanh, có thể tái sử dụng và cho phép bạn ủy thác một số logic khó khăn cho các thành phần thư viện tiêu chuẩn được thử nghiệm tốt. Xem xét ví dụ:

import itertools
import operator

def most_common(L):
  # get an iterable of (item, iterable) pairs
  SL = sorted((x, i) for i, x in enumerate(L))
  # print 'SL:', SL
  groups = itertools.groupby(SL, key=operator.itemgetter(0))
  # auxiliary function to get "quality" for an item
  def _auxfun(g):
    item, iterable = g
    count = 0
    min_index = len(L)
    for _, where in iterable:
      count += 1
      min_index = min(min_index, where)
    # print 'item %r, count %r, minind %r' % (item, count, min_index)
    return count, -min_index
  # pick the highest-count/earliest item
  return max(groups, key=_auxfun)[0]

Điều này có thể được viết chính xác hơn, tất nhiên, nhưng tôi đang nhắm đến sự rõ ràng tối đa. Hai tuyên bố print có thể không được đưa ra để thấy tốt hơn các máy móc hoạt động; Ví dụ, với các bản in chưa được đưa ra:

print most_common(['goose', 'duck', 'duck', 'goose'])

emits:

SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
item 'duck', count 2, minind 1
item 'goose', count 2, minind 0
goose

Như bạn thấy, SL là danh sách các cặp, mỗi cặp một mục theo sau là chỉ mục của mục trong danh sách ban đầu (để thực hiện điều kiện chính, nếu các mục "phổ biến nhất" có cùng số lượng cao nhất> 1, kết quả phải là cái sớm nhất).

print most_common(['goose', 'duck', 'duck', 'goose'])
0 Các nhóm chỉ theo mục (thông qua
print most_common(['goose', 'duck', 'duck', 'goose'])
1). Hàm phụ trợ, được gọi là một lần mỗi nhóm trong quá trình tính toán
print most_common(['goose', 'duck', 'duck', 'goose'])
2, nhận và trong nội bộ giải nén một nhóm - một tuple với hai mục
print most_common(['goose', 'duck', 'duck', 'goose'])
3 trong đó các mục của Iterable cũng là các bộ dữ liệu hai mục,
print most_common(['goose', 'duck', 'duck', 'goose'])
4 [[các mục của SL]].

Sau đó, chức năng phụ trợ sử dụng một vòng lặp để xác định cả số lượng các mục nhập trong ITable của nhóm và chỉ mục gốc tối thiểu; Nó trả về những người như kết hợp "Khóa chất lượng", với chỉ số tối thiểu được thay đổi để hoạt động

print most_common(['goose', 'duck', 'duck', 'goose'])
2 sẽ xem xét "tốt hơn" những mục xảy ra trước đó trong danh sách ban đầu.

Mã này có thể đơn giản hơn nhiều nếu nó lo lắng ít hơn một chút về các vấn đề lớn về thời gian và không gian, ví dụ ....:

def most_common(L):
  groups = itertools.groupby(sorted(L))
  def _auxfun((item, iterable)):
    return len(list(iterable)), -L.index(item)
  return max(groups, key=_auxfun)[0]

cùng một ý tưởng cơ bản, chỉ thể hiện đơn giản và nhỏ gọn hơn ... nhưng, than ôi, một không gian phụ O (n) thêm (để thể hiện thời gian của các nhóm) và thời gian O (n bình phương) (để có được

print most_common(['goose', 'duck', 'duck', 'goose'])
7 của mỗi mục ). Mặc dù tối ưu hóa sớm là gốc rễ của tất cả các điều ác trong lập trình, nhưng cố tình chọn cách tiếp cận O (n bình phương) khi có sẵn một O (n log n), có sẵn quá nhiều so với hạt của khả năng mở rộng!-)

Cuối cùng, đối với những người thích "oneliners" với sự rõ ràng và hiệu suất, phiên bản 1 liner thưởng với tên được tạo ra phù hợp :-).

from itertools import groupby as g
def most_common_oneliner(L):
  return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Examples:  

    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog

    Bàn luận Naive Approach
    This is a brute force approach in which we make use of for loop to count the frequency of each element. If the current frequency is greater than the previous frequency, update the counter and store the element. 

    Python3

    Cho một danh sách, tìm yếu tố thường xuyên nhất trong đó. Nếu có nhiều yếu tố xuất hiện số lần tối đa, hãy in bất kỳ một trong số chúng.

    Cách tiếp cận số 1: Cách tiếp cận ngây thơ này là một cách tiếp cận lực lượng vũ phu, trong đó chúng ta sử dụng vòng lặp để đếm tần số của từng phần tử. Nếu tần số hiện tại lớn hơn tần số trước đó, hãy cập nhật bộ đếm và lưu trữ phần tử. & NBSP;

    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    8
    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    9
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    1

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    3
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    5

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0____________
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    5
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    2

    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    9
    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    5
    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    6

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    4
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    5
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    6
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    8

    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    9
    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    0
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    3

    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    3
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    0

    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    4

    printitertools4

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0itertools6

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    7
    Approach #2 : Pythonic Naive approach
    Make a set of the list so that the duplicate elements are deleted. Then find the highest count of occurrences of each element in the set and thus, we find the maximum out of it. 

    Python3

    Cho một danh sách, tìm yếu tố thường xuyên nhất trong đó. Nếu có nhiều yếu tố xuất hiện số lần tối đa, hãy in bất kỳ một trong số chúng.

    Cách tiếp cận số 1: Cách tiếp cận ngây thơ này là một cách tiếp cận lực lượng vũ phu, trong đó chúng ta sử dụng vòng lặp để đếm tần số của từng phần tử. Nếu tần số hiện tại lớn hơn tần số trước đó, hãy cập nhật bộ đếm và lưu trữ phần tử. & NBSP;

    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    4

    printitertools4

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0itertools6

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    7
    Approach #3 : Using Counter
    Make use of Python Counter which returns count of each element in the list. Thus, we simply find the most common element by using most_common() method. 

    Python3

    Is

    Cho một danh sách, tìm yếu tố thường xuyên nhất trong đó. Nếu có nhiều yếu tố xuất hiện số lần tối đa, hãy in bất kỳ một trong số chúng.

    Cách tiếp cận số 1: Cách tiếp cận ngây thơ này là một cách tiếp cận lực lượng vũ phu, trong đó chúng ta sử dụng vòng lặp để đếm tần số của từng phần tử. Nếu tần số hiện tại lớn hơn tần số trước đó, hãy cập nhật bộ đếm và lưu trữ phần tử. & NBSP;

    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    8
    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    9
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    1

    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    4

    printitertools4

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0itertools6

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    7
    By finding mode 
    Finding most frequent element means finding mode of the list. Hence, we use mode method from statistics.  

    Python3

    Is

    & nbsp; & nbsp; Cách tiếp cận #2: Phương pháp tiếp cận ngây thơ Pythonic Một tập hợp của danh sách để các yếu tố trùng lặp bị xóa. Sau đó tìm số lần xuất hiện cao nhất của mỗi phần tử trong tập hợp và do đó, chúng tôi tìm thấy mức tối đa của nó. & Nbsp;

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    2print4print5print4___

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    65
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0itertools6

    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    4

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    7

    Is Using Python dictionary
    Use python dictionary to save element as a key and its frequency as the value, and thus find the most frequent element.  

    Python3

    Cho một danh sách, tìm yếu tố thường xuyên nhất trong đó. Nếu có nhiều yếu tố xuất hiện số lần tối đa, hãy in bất kỳ một trong số chúng.

    Cách tiếp cận số 1: Cách tiếp cận ngây thơ này là một cách tiếp cận lực lượng vũ phu, trong đó chúng ta sử dụng vòng lặp để đếm tần số của từng phần tử. Nếu tần số hiện tại lớn hơn tần số trước đó, hãy cập nhật bộ đếm và lưu trữ phần tử. & NBSP;

    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    8
    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    9
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    1

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    3
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    5

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0____________
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    5
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    2

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    4
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    5
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    6
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    8

    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    9
    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    0
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    3

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    31

    from itertools import groupby as g
    def most_common_oneliner(L):
      return max(g(sorted(L)), key=lambda(x, v):(len(list(v)),-L.index(x)))[0]
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    7
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    4

    printitertools4

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0itertools6

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    7
    Using pandas library. 
    Incase of multiple values getting repeated. Print all values.

    Python3

    Is

    & nbsp; & nbsp; Cách tiếp cận #2: Phương pháp tiếp cận ngây thơ Pythonic Một tập hợp của danh sách để các yếu tố trùng lặp bị xóa. Sau đó tìm số lần xuất hiện cao nhất của mỗi phần tử trong tập hợp và do đó, chúng tôi tìm thấy mức tối đa của nó. & Nbsp;

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    70
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    72
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    73
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    74
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    0
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    76

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    2
    Input : [2, 1, 2, 2, 1, 3]
    Output : 2
    
    Input : ['Dog', 'Cat', 'Dog']
    Output : Dog
    6
    print most_common(['goose', 'duck', 'duck', 'goose'])
    
    2print4print5print4___

    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    90
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    88
    def most_common(L):
      groups = itertools.groupby(sorted(L))
      def _auxfun((item, iterable)):
        return len(list(iterable)), -L.index(item)
      return max(groups, key=_auxfun)[0]
    
    2
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    4
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    90
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    73
    SL: [('duck', 1), ('duck', 2), ('goose', 0), ('goose', 3)]
    item 'duck', count 2, minind 1
    item 'goose', count 2, minind 0
    goose
    
    96

    Các


    Làm thế nào để tôi tìm thấy yếu tố phổ biến nhất trong danh sách Python?

    Python3. Sử dụng bộ đếm Python trả về số lượng của từng yếu tố trong danh sách. Do đó, chúng tôi chỉ đơn giản tìm thấy phần tử phổ biến nhất bằng cách sử dụng phương thức mest_common ().using most_common() method.

    Làm cách nào để tìm thấy số phổ biến nhất trong danh sách?

    Nếu bạn chỉ muốn tìm thấy số phổ biến nhất trong danh sách, bạn có thể sử dụng một công thức đơn giản.Chọn một ô trống, ở đây là C1, nhập công thức này = mode (A1: A13), sau đó nhấn phím Enter để có được số phổ biến nhất trong danh sách.=MODE(A1:A13), and then press Enter key to get the most common number in the list.

    Làm thế nào để tôi tìm thấy số phổ biến thứ hai trong danh sách Python?

    Đếm (Số)in (str (most_common [0]) + "được liệt kê" + str (most_common [1]) + "lần!")

    Làm thế nào để bạn tìm thấy sự xuất hiện của một yếu tố trong một danh sách?

    Phương pháp 4: Số lần xuất hiện của một phần tử trong danh sách bằng toán tử Countof ().Countof () được sử dụng để đếm số lần xuất hiện của B trong a.Nó đếm số lần xuất hiện của giá trị.Using countof() Operator. countOf() is used for counting the number of occurrences of b in a. It counts the number of occurrences of value.