【问题标题】:"pythonic" way to fill bag of words“pythonic”填充词袋的方式
【发布时间】:2016-10-13 20:05:57
【问题描述】:

我有一个单词列表,列表中有大约 273000 个单词Word_array 大约有17000个唯一词,它们存储在Word_arrayU

我想为每一个计数

#make bag of worsds   
Word_arrayU = np.unique(Word_array)
wordBag = [['0','0'] for _ in range(len(Word_array))] #prealocate necessary space
i=0
while i< len(Word_arrayU): #for each unique word
    wordBag[i][0] = Word_arrayU[i]
    #I think this is the part that takes a long time.  summing up a list comprehension with a conditional.  Just seems sloppy
    wordBag[i][1]=sum([1 if x == Word_arrayU[i] else 0 for x in Word_array])
    i=i+1

用条件总结列表理解。只是看起来很草率;有更好的方法吗?

【问题讨论】:

  • 为什么不使用collections.Counter
  • 您是否考虑过使用集合中的计数器?它的唯一目的是为您提供一个字典,其中键是可迭代项中的项,值是可迭代项中这些项的计数...
  • @jonrsharpe 我喜欢我们的想法......
  • len([x for x in Word_array if x == Word_arrayU[i]])
  • len(filter(Word_arrayU[i], Word_array))

标签: python string counter


【解决方案1】:
from collections import Counter
counter = Counter(Word_array)
the_count_of_some_word = counter["some_word"]

#printing the counts
for word, count in counter.items():
   print("{} appears {} times.".format(word, count)

【讨论】:

    【解决方案2】:

    由于您已经在使用numpy.unique,只需在唯一调用中设置return_counts=True

    import numpy as np
    
    unique,  count = np.unique(Word_array, return_counts=True)
    

    这将为您提供两个数组、唯一元素及其计数:

    n [10]: arr = [1,3,2,11,3,4,5,2,3,4]
    
    In [11]: unique,  count = np.unique(arr, return_counts=True)
    
    In [12]: unique
    Out[12]: array([ 1,  2,  3,  4,  5, 11])
    
    In [13]: count
    Out[13]: array([1, 2, 3, 2, 1, 1])
    

    【讨论】:

      【解决方案3】:

      基于@jonrsharpe 的建议...

      from collections import Counter
      
      words = Counter()
      
      words['foo'] += 1
      words['foo'] += 1
      words['bar'] += 1
      

      输出

      Counter({'bar': 1, 'foo': 2})
      

      这真的很方便,因为您不必初始化单词。

      您也可以直接从单词列表中初始化:

      Counter(['foo', 'foo', 'bar'])
      

      输出

      Counter({'bar': 1, 'foo': 2})
      

      【讨论】:

        【解决方案4】:

        在 python 3 中有一个内置的 list.count 函数。例如:

        >>> h = ["a", "b", "a", "a", "c"]
        >>> h.count("a")
        3
        >>> 
        

        因此,您可以通过以下方式提高效率:

        Word_arrayU = np.unique(Word_array)
        wordBag = []
        for uniqueWord in Word_arrayU:
            wordBag.append([uniqueWord, Word_array.count(uniqueWord)])
        

        【讨论】:

          【解决方案5】:

          我不知道大多数“Pythonic”,但绝对最简单的方法是使用collections.Counter

          from collections import Counter
          
          Word_array = ["word1", "word2", "word3", "word1", "word2", "word1"]
          
          wordBag = Counter(Word_array).items()
          

          【讨论】:

          • 糟糕的建议 - 数两次?如果你 - 出于某种模糊的原因 - 想要元组,Counter(Word_array).items() 可以解决问题
          • 如果你在相同的数据上调用一个函数两次——这是不好的做法。由于 OP 正在谈论 237,000 个单词的列表 - 是的,这很糟糕。
          【解决方案6】:

          如果您想要一个效率较低(比Counter)但更透明的解决方案,您可以使用collections.defaultdict

          from collections import defaultdict
          my_counter = defaultdict(int)
          for word in word_array:
              my_counter[word] += 1
          

          【讨论】:

          • 他为什么要使用 defaultdict 而不是 Counter?
          猜你喜欢
          • 1970-01-01
          • 2019-05-15
          • 2013-11-10
          • 1970-01-01
          • 2012-09-05
          • 1970-01-01
          • 1970-01-01
          • 2013-03-09
          • 1970-01-01
          相关资源
          最近更新 更多