2010-08-29 14 views
23

को देखते हुए निम्न सूचीकिसी सूची के सबसे आम तत्व कैसे ढूंढें?

['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 
'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 
'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 
'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 
'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 
'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 
'Moon', 'to', 'rise.', ''] 

मैं कितनी बार प्रत्येक शब्द प्रकट होता है गिनती और फिर भी मैं केवल शीर्ष तीन कि पहला अक्षर बड़ा है खोजने के लिए देख रहा हूँ शीर्ष 3.

प्रदर्शित करने के लिए कोशिश कर रहा हूँ और उन सभी शब्दों को अनदेखा करें जिनके पास पहला अक्षर पूंजीकृत नहीं है।

मुझे यकीन है कि वहाँ इस तुलना में एक बेहतर तरीका है हूँ, लेकिन मेरा विचार निम्न करने के लिए किया गया था:

  1. uniquewords
  2. नामक एक और सूची में सूची में पहले शब्द डाल पहला शब्द हटा सकते हैं और मूल सूची
  3. अद्वितीय शब्दों में नया पहला शब्द जोड़ें
  4. पहले शब्द को हटाएं और इसकी सभी मूल सूची से डुप्लिकेट करें।
  5. आदि
  6. ...
  7. जब तक मूल सूची खाली ....
  8. गिनती कितनी बार uniquewords में प्रत्येक शब्द मूल सूची में दिखाई देता
  9. खोजने के शीर्ष 3 और प्रिंट है
+0

'help' नहीं एक उपयोगी टैग है। – SilentGhost

+0

मुझे 'बिल्लियों' संदर्भ पसंद है! – dls

उत्तर

14

यदि आप पाइथन के पहले संस्करण का उपयोग कर रहे हैं या आपके पास अपना खुद का शब्द काउंटर रोल करने का बहुत अच्छा कारण है (मैं इसे सुनना चाहता हूं!), तो आप dict का उपयोग करके निम्नलिखित दृष्टिकोण को आजमा सकते हैं।

Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> word_list = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.', ''] 
>>> word_counter = {} 
>>> for word in word_list: 
...  if word in word_counter: 
...   word_counter[word] += 1 
...  else: 
...   word_counter[word] = 1 
... 
>>> popular_words = sorted(word_counter, key = word_counter.get, reverse = True) 
>>> 
>>> top_3 = popular_words[:3] 
>>> 
>>> top_3 
['Jellicle', 'Cats', 'and'] 

शीर्ष टिप: इंटरैक्टिव अजगर interpretor आप इस तरह एक एल्गोरिथ्म के साथ खेलना चाहते हैं जब भी अपने दोस्त है। बस इसे टाइप करें और इसे देखें, रास्ते में तत्वों का निरीक्षण करें।

>>> counter = {} 
>>> for i in l: counter[i] = counter.get(i, 0) + 1 
>>> sorted([ (freq,word) for word, freq in counter.items() ], reverse=True)[:3] 
[(6, 'Jellicle'), (5, 'Cats'), (3, 'to')] 

पूरा नमूना:

+0

इसके लिए धन्यवाद ... लेकिन मैं इसे कैसे कर सकता हूं, इसलिए यह केवल दूसरे पत्र को अनदेखा कर रहा है, पहले अक्षर के साथ शब्दों को देखता है। ps। यदि एक शब्द कई बार प्रकट होता है, कभी-कभी पूंजीकृत और अन्य बार कैप्चरिलाइज्ड नहीं होता है, तो केवल उदाहरणों की गणना करें जब शब्द का पहला अक्षर पूंजी है। – user434180

+1

... तो यह होमवर्क की तरह बहुत कुछ लगता है (और सवाल इस तरह चिह्नित किया जाना चाहिए)। बस 'word_counter' को लोअर-केस अक्षर से शुरू होने वाले किसी भी शब्द को न जोड़ें। यदि आप यह दिखाने के लिए अपना प्रश्न अपडेट करते हैं कि (ए) यह एक आवश्यकता है और (बी) कि आपने इसे स्वयं करने की कोशिश की है, तो लोगों की मदद करने की अधिक संभावना है। – Johnsyweb

+0

@ जॉन्सवेवे - इस विषय पर, मैं शब्दों के नाम दिखाने के लिए 'लोकप्रिय_वर्ड्स' सूची के चारों ओर फिर से शुरू करने की कोशिश कर रहा हूं और उनके बगल में उनकी गिनती भी ... मुझे अब तक कोई भाग्य नहीं है, क्या आप इंगित कर सकते हैं मुझे सही दिशा में? अग्रिम धन्यवाद – drew

55

from collections import Counter 
words_to_count = (word for word in word_list if word[:1].isupper()) 
c = Counter(words_to_count) 
print c.most_common(3) 

परिणाम::

0 अजगर में 2.7 और इसके बाद के संस्करण वहाँ एक वर्ग Counter कहा जाता है जो आपकी मदद कर सकता है
[('Jellicle', 6), ('Cats', 5), ('And', 2)] 

I am quite new to programming so please try and do it in the most barebones fashion.

इसके बजाय आप कुंजी एक शब्द जा रहा है और मूल्य है कि शब्द के लिए गिनती होने के साथ एक शब्दकोश का उपयोग कर ऐसा कर सकता है। सबसे पहले शब्दकोष में उन्हें जोड़कर शब्दों को दोहराएं, यदि वे मौजूद नहीं हैं, या अन्यथा मौजूद होने पर शब्द के लिए गिनती बढ़ाना। फिर शीर्ष तीन को खोजने के लिए आप या तो सरल O(n*log(n)) सॉर्टिंग एल्गोरिदम का उपयोग कर सकते हैं और परिणाम से पहले तीन तत्व ले सकते हैं, या आप O(n) एल्गोरिदम का उपयोग कर सकते हैं जो सूची को केवल शीर्ष तीन तत्वों को याद करने के बाद स्कैन करता है।

शुरुआती लोगों के लिए एक महत्वपूर्ण अवलोकन यह है कि उद्देश्य के लिए डिज़ाइन किए गए बिल्टिन कक्षाओं का उपयोग करके आप स्वयं को बहुत अधिक काम बचा सकते हैं और/या बेहतर प्रदर्शन प्राप्त कर सकते हैं। मानक लाइब्रेरी और इसकी पेशकश की जाने वाली सुविधाओं से परिचित होना अच्छा होता है।

+0

'काउंटर (एलएसटी) '? – SilentGhost

+0

मुझे आयात त्रुटि क्यों मिल रही है (पायथन 2.6.1 पर)? 'आयात त्रुटि: नाम आयात नहीं कर सकता काउंटर' – abhiomkar

+4

@abhiomkar: क्योंकि पायथन 2.6.1 पायथन 2.7 या ऊपर नहीं है। –

1

आसान तरीका ऐसा करने का किया जाएगा (अपनी सूची संभालने 'l' में है)

>>> l = ['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 'Moon', 'to', 'rise.', ''] 
>>> counter = {} 
>>> for i in l: counter[i] = counter.get(i, 0) + 1 
... 
>>> counter 
{'and': 3, '': 1, 'merry': 1, 'rise.': 1, 'small;': 1, 'Moon': 1, 'cheerful': 1, 'bright': 1, 'Cats': 5, 'are': 3, 'have': 2, 'bright,': 1, 'for': 1, 'their': 1, 'rather': 1, 'when': 1, 'to': 3, 'airs': 1, 'black': 2, 'They': 1, 'practise': 1, 'caterwaul.': 1, 'pleasant': 1, 'hear': 1, 'they': 1, 'white,': 1, 'wait': 1, 'And': 2, 'like': 1, 'Jellicle': 6, 'eyes;': 1, 'the': 1, 'faces,': 1, 'graces': 1} 
>>> sorted([ (freq,word) for word, freq in counter.items() ], reverse=True)[:3] 
[(6, 'Jellicle'), (5, 'Cats'), (3, 'to')] 

सरल मैं में लगभग हर काम कर मतलब के साथ पायथन का संस्करण।

अगर आप इस नमूने में इस्तेमाल किया कार्यों में से कुछ समझ में नहीं आता, तो आप हमेशा यह दुभाषिया में कर सकते हैं (जैसा कि ऊपर कोड पेस्ट करने के बाद):

>>> help(counter.get) 
>>> help(sorted) 
4

nltk भाषा का एक बहुत के लिए सुविधाजनक है प्रसंस्करण सामान यह आवृत्ति वितरण के लिए तरीकों में बनाया गया है कुछ की तरह:।

from collections import Counter 
words=["i", "love", "you", "i", "you", "a", "are", "you", "you", "fine", "green"] 
most_common_words= [word for word, word_count in Counter(words).most_common(3)] 
print most_common_words 

इस प्रिंट:

['you', 'i', 'a'] 

में 3

import nltk 
fdist = nltk.FreqDist(your_list) # creates a frequency distribution from a list 
most_common = fdist.max() # returns a single element 
top_three = fdist.keys()[:3] # returns a list 
12

सिर्फ सबसे आम शब्दों से युक्त एक सूची वापस जाने के लिए "most_common(3)", मुद्रित करने के लिए आइटम की संख्या निर्दिष्ट करता है। Counter(words).most_common() प्रत्येक सदस्य के साथ टुपल्स की एक सूची देता है जिसमें पहले सदस्य के रूप में शब्द होता है और दूसरी सदस्य के रूप में आवृत्ति होती है। टुपल्स को शब्द की आवृत्ति द्वारा आदेश दिया जाता है।

`most_common = [item for item in Counter(words).most_common()] 
print(str(most_common)) 
[('you', 4), ('i', 2), ('a', 1), ('are', 1), ('green', 1), ('love',1), ('fine', 1)]` 

"word for word, word_counter in", टपल का केवल पहला सदस्य निकालता है।

+0

क्या अधिकांश_common फ़ंक्शन के माध्यम से घटना की संख्या को वापस करना संभव है? –

+1

हां, लगभग एक शुरुआती, यह कर सकता है, मुझे आपको यह दिखाने के लिए जवाब संपादित करने दें कि कैसे – unlockme

1

@ मार्क बॉयर्स का उत्तर सबसे अच्छा है, लेकिन यदि आप पाइथन < 2.7 (लेकिन कम से कम 2.5, जो इन दिनों बहुत पुराना है) के संस्करण पर हैं, तो आप काउंटर क्लास कार्यक्षमता को डिफ़ॉल्ट रूप से डिफ़ॉल्ट रूप से दोहरा सकते हैं (अन्यथा, पाइथन < 2.5 के लिए, डी [i] + = 1 से पहले कोड की तीन अतिरिक्त पंक्तियों की आवश्यकता होती है, जैसा कि @ जॉनीसवेब के उत्तर में)।

from collections import defaultdict 
class Counter(): 
    ITEMS = [] 
    def __init__(self, items): 
     d = defaultdict(int) 
     for i in items: 
      d[i] += 1 
     self.ITEMS = sorted(d.iteritems(), reverse=True, key=lambda i: i[1]) 
    def most_common(self, n): 
     return self.ITEMS[:n] 

उसके बाद, आप, वास्तव में मार्क बायर्स के जवाब के रूप में वर्ग का उपयोग अर्थात्:

words_to_count = (word for word in word_list if word[:1].isupper()) 
c = Counter(words_to_count) 
print c.most_common(3) 
1

एक साधारण, दो लाइन इस का हल है, जो किसी भी अतिरिक्त मॉड्यूल की आवश्यकता नहीं है निम्नलिखित कोड है :

lst = ['Jellicle', 'Cats', 'are', 'black', 'and','white,', 
     'Jellicle', 'Cats','are', 'rather', 'small;', 'Jellicle', 
     'Cats', 'are', 'merry', 'and','bright,', 'And', 'pleasant',  
     'to','hear', 'when', 'they', 'caterwaul.','Jellicle', 
     'Cats', 'have','cheerful', 'faces,', 'Jellicle', 
     'Cats','have', 'bright', 'black','eyes;', 'They', 'like', 
     'to', 'practise','their', 'airs', 'and', 'graces', 'And', 
     'wait', 'for', 'the', 'Jellicle','Moon', 'to', 'rise.', ''] 

lst_sorted=sorted([ss for ss in set(lst) if len(ss)>0 and ss.istitle()], 
        key=lst.count, 
        reverse=True) 
print lst_sorted[0:3] 

आउटपुट:

['Jellicle', 'Cats', 'And'] 

स्क्वायर ब्रैकेट्स में शब्द सूची में सभी अद्वितीय तार लौटाता है, जो खाली नहीं हैं और पूंजी पत्र से शुरू होते हैं। sorted() फ़ंक्शन फिर उन्हें क्रमबद्ध करता है कि वे सूची में कितनी बार दिखाई देते हैं (lst.count कुंजी का उपयोग करके) रिवर्स ऑर्डर में।

0

आप गणना उपयोग कर रहे हैं, या अपने स्वयं गणना शैली dict बनाया है और आइटम का नाम है और यह की गिनती दिखाना चाहते है, तो आप तो जैसे शब्दकोश आसपास पुनरावृति कर सकते हैं:

top_10_words = Counter(my_long_list_of_words) 
# Iterate around the dictionary 
for word in top_10_words: 
     # print the word 
     print word[0] 
     # print the count 
     print word[1] 

या एक टेम्पलेट में इस के माध्यम से पुनरावृति करने के लिए:

{% for word in top_10_words %} 
     <p>Word: {{ word.0 }}</p> 
     <p>Count: {{ word.1 }}</p> 
{% endfor %} 

आशा इस मदद करता है किसी को

3

यह सिर्फ इस is't ....

word_list=['Jellicle', 'Cats', 'are', 'black', 'and', 'white,', 'Jellicle', 'Cats', 
'are', 'rather', 'small;', 'Jellicle', 'Cats', 'are', 'merry', 'and', 
'bright,', 'And', 'pleasant', 'to', 'hear', 'when', 'they', 'caterwaul.', 
'Jellicle', 'Cats', 'have', 'cheerful', 'faces,', 'Jellicle', 'Cats', 
'have', 'bright', 'black', 'eyes;', 'They', 'like', 'to', 'practise', 
'their', 'airs', 'and', 'graces', 'And', 'wait', 'for', 'the', 'Jellicle', 
'Moon', 'to', 'rise.', ''] 

from collections import Counter 
c = Counter(word_list) 
c.most_common(3) 

कौन सा चाहिए उत्पादन

[('Jellicle', 6), ('Cats', 5), ('are', 3)]

संबंधित मुद्दे