खोज…


सभी वस्तुओं की सभी घटनाओं को एक पुनरावृत्त में गिनना: संग्रह। मुठभेड़

from collections import Counter

c = Counter(["a", "b", "c", "d", "a", "b", "a", "c", "d"])
c
# Out: Counter({'a': 3, 'b': 2, 'c': 2, 'd': 2})
c["a"]
# Out: 3

c[7]     # not in the list (7 occurred 0 times!)
# Out: 0

collections.Counter । मुठभेड़ किसी भी चलने के लिए इस्तेमाल किया जा सकता है और हर तत्व के लिए हर घटना को मायने रखता है।

नोट : एक अपवाद यदि एक dict या अन्य collections.Mapping मैपिंग-समान श्रेणी दी जाती है, तो यह उनकी गणना नहीं करेगा, बल्कि यह इन मूल्यों के साथ एक काउंटर बनाता है:

Counter({"e": 2})
# Out: Counter({"e": 2})

Counter({"e": "e"})        # warning Counter does not verify the values are int
# Out: Counter({"e": "e"})

सबसे आम मूल्य (-s) प्राप्त करना: संग्रह। मुठभेड़। अत्यंत_ असामान्य ()

Mapping की कुंजी की गणना collections.Counter साथ संभव नहीं है। Mapping लेकिन हम मूल्यों की गणना कर सकते हैं :

from collections import Counter
adict = {'a': 5, 'b': 3, 'c': 5, 'd': 2, 'e':2, 'q': 5}
Counter(adict.values())
# Out: Counter({2: 2, 3: 1, 5: 3})

सबसे सामान्य तत्व सबसे अधिक most_common -method:

# Sorting them from most-common to least-common value:
Counter(adict.values()).most_common()
# Out: [(5, 3), (2, 2), (3, 1)]

# Getting the most common value
Counter(adict.values()).most_common(1)
# Out: [(5, 3)]

# Getting the two most common values
Counter(adict.values()).most_common(2)
# Out: [(5, 3), (2, 2)]

अनुक्रम में एक आइटम की घटनाओं की गणना: list.count () और tuple.count ()

alist = [1, 2, 3, 4, 1, 2, 1, 3, 4]
alist.count(1)
# Out: 3

atuple = ('bear', 'weasel', 'bear', 'frog')
atuple.count('bear')
# Out: 2
atuple.count('fox')
# Out: 0

स्ट्रिंग में एक प्रतिस्थापन की घटनाओं की गणना: str.count ()

astring = 'thisisashorttext'
astring.count('t')
# Out: 4

यह एक वर्ण से लंबे समय तक सब्सट्रिंग के लिए भी काम करता है:

astring.count('th')
# Out: 1
astring.count('is')
# Out: 2
astring.count('text')
# Out: 1

जो collections.Counter साथ संभव नहीं होगा। मुठभेड़ जो केवल एकल वर्णों की गणना करता है:

from collections import Counter
Counter(astring)
# Out: Counter({'a': 1, 'e': 1, 'h': 2, 'i': 2, 'o': 1, 'r': 1, 's': 3, 't': 4, 'x': 1})

सुन्न सरणी में गिनती की घटनाओं

मूल्य की घटनाओं को एक संख्यात्मक सरणी में गिनने के लिए। यह काम करेगा:

>>> import numpy as np
>>> a=np.array([0,3,4,3,5,4,7])
>>> print np.sum(a==3)
2

तर्क यह है कि बूलियन स्टेटमेंट एक एरे का निर्माण करता है जहां अनुरोधित मूल्यों की सभी घटनाएं 1 हैं और अन्य सभी शून्य हैं। तो संक्षेप में इन घटनाओं की संख्या देता है। यह किसी भी आकृति या dtype के सरणियों के लिए काम करता है।

सुन्नत में सभी अद्वितीय मूल्यों की घटनाओं को गिनने के लिए मैं दो विधियों का उपयोग करता हूं। अनोखा और द्विअर्थी। अद्वितीय स्वचालित रूप से बहुआयामी सरणियों को समतल करता है, जबकि बींकाउंट केवल 1 डी सरणियों के साथ काम करता है जिसमें केवल सकारात्मक पूर्णांक होते हैं।

>>> unique,counts=np.unique(a,return_counts=True)
>>> print unique,counts # counts[i] is equal to occurrences of unique[i] in a
[0 3 4 5 7] [1 2 2 1 1]
>>> bin_count=np.bincount(a)
>>> print bin_count # bin_count[i] is equal to occurrences of i in a
[1 0 0 2 2 1 0 1] 

यदि आपका डेटा numpy arrays है, तो आम तौर पर numpy विधियों का उपयोग करना बहुत तेजी से होता है, फिर अपने डेटा को जेनेरिक विधियों में परिवर्तित करना।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow