English 中文(简体)
如何发现一字在阵列中出现多少次? 甲型六氯环己烷
原标题:How to find how many times a word appears in a array? Python [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
item frequency count in python

速效项目

你们是如何发现一字在阵列中出现多少次?

我有一阵列,有大约5,000字案文,我想找到阵列中“帮助”一词的多少次。 如何这样做?

阵列储存在X,因此,我的法典认为:

x = [...]
word = "help"

然后,我知道,用什么来使“帮助”的次数出现在x中。

感谢您的帮助!

问题回答
>>> import collections
>>> print collections.Counter([ a ,  word ,  is ,  a ,  thing ,  that ,  is ,  countable ])
Counter({ a : 2,  is : 2,  word : 1,  that : 1,  countable : 1,  thing : 1})

http://docs.python.org/library/collections.html# Collections.Counter>Counter

Based on your edit, where each element in the list is a letter instead of the full word, then:

>>> import re
>>> letters = 
[ i ,  n ,  e ,  e ,  d ,  s ,  o ,  m ,  e ,  h ,  e ,  l ,  p ,  h ,  e ,  l ,  p ,  m ,  e ,  p ,  l ,  e ,  a ,  s ,  e ,  I ,  n ,  e ,  e ,  d ,  h ,  e ,  l ,  p ]
>>> len(re.findall( help , "".join(letters)))
3

As @sberry has depicted, Counter would server the purpose, but in case you are only searching a single word once and not interested to get the occurrence of all the words, you can use a simpler tool for the purpose

(我从树林中举出了例子)

既然有一言之词,你可以使用名单的<条码>(<>>><>>代码/代码>方法。

>>> list_of_words=[ a ,  word ,  is ,  a ,  thing ,  that ,  is ,  countable ]
>>> list_of_words.count( is )
2

你的评论表明,你可能有兴趣寻找一份特征清单。 ......

letters =
[ i ,  n ,  e ,  e ,  d ,  s ,  o ,  m ,  e ,  h ,  e ,  l ,  p ,  h ,  e ,  l ,  p ,  m ,  e ,  p ,  l ,  e ,  a ,  s ,  e ,  I ,  n ,  e ,  e ,  d ,  h ,  e ,  l ,  p ]

你们也可以利用通过压缩所有特性而形成的后遗体。

>>>   .join(letters).count( help )
3

如果这些词语被打碎,<代码>收集。 反

>>> def count_words_in_jumbled(jumbled,word):
    jumbled_counter = collections.Counter(jumbled)
    word_counter = collections.Counter(word)
    return min(v /word_counter[k] for k,v in jumbled_counter.iteritems() if k in word)

>>> count_words_in_jumbled([ h , e , l , l , h , e , l , l , h , e , l ], hel )
3
>>> count_words_in_jumbled([ h , e , l , l , h , e , l , l , h , e , l ], hell )
2
>>> count_words_in_jumbled([ h , x , e , y , l , u , p ] , help )
1
nhelps = len(  .join(charlist).split( help )[1:]




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签