【问题标题】:Python: Count of occurrences in dict from another listPython:来自另一个列表的dict中出现的计数
【发布时间】:2021-03-26 14:22:54
【问题描述】:

我正在尝试根据感兴趣的单词子集计算单词在 dict 列中存在的次数。

首先我导入我的数据

products = graphlab.SFrame('amazon_baby.gl/')
products['word_count'] = graphlab.text_analytics.count_words(products['review'])
products.head(5)

数据可以在这里找到:https://drive.google.com/open?id=0BzbhZp-qIglxM3VSVWRsVFRhTWc

然后我创建我感兴趣的单词列表:

words = ['awesome', 'great', 'fantastic']

我想计算“words”中每个单词在 products['word_count'] 中出现的次数。

我不喜欢使用 graphlab。这是同事向我建议的。

【问题讨论】:

标签: python word-count graphlab sframe


【解决方案1】:

好吧,我不太清楚您所说的“在 dict 列中”是什么意思。 如果是列表:

import operator
dictionary={'texts':['red blue blue','red black','blue white white','red','white','black','blue red']}
words=['red','white','blue']
freqs=dict()
for t in dictionary['texts']:
    for w in words:
        try:
             freqs[w]+=t.count(w)
        except:
            freqs[w]=t.count(w)
top_words = sorted(freqs.items(), key=operator.itemgetter(1),reverse=True)

如果只是一个文本:

import operator
dictionary={'text':'red blue blue red black blue white white red white black blue red'}
words=['red','white','blue']
freqs=dict()
for w in words:
    try:
        freqs[w]+=dictionary['text'].count(w)
    except:
        freqs[w]=dictionary['text'].count(w)
top_words = sorted(freqs.items(), key=operator.itemgetter(1),reverse=True) 

【讨论】:

    【解决方案2】:

    如果你想计算单词的出现次数,一个快速的方法是使用来自collectionsCounterobject

    例如:

    In [3]: from collections import Counter
    In [4]: c = Counter(['hello', 'world'])
    
    In [5]: c
    Out[5]: Counter({'hello': 1, 'world': 1})
    

    能否显示products.head(5) 命令的输出?

    【讨论】:

      【解决方案3】:

      如果您坚持使用 graphlab(或 SFrame),请使用 SArray.dict_trim_by_keys 方法。文档在这里:https://dato.com/products/create/docs/generated/graphlab.SArray.dict_trim_by_keys.html

      import graphlab as gl
      sf = gl.SFrame({'review': ['what a good book', 'terrible book']})
      sf['word_bag'] = gl.text_analytics.count_words(sf['review'])
      
      keywords = ['good', 'book']
      sf['key_words'] = sf['word_bag'].dict_trim_by_keys(keywords, exclude=False)
      print sf
      
      +------------------+---------------------+---------------------+
      |      review      |       word_bag      |      key_words      |
      +------------------+---------------------+---------------------+
      | what a good book | {'a': 1, 'good':... | {'good': 1, 'boo... |
      |  terrible book   | {'book': 1, 'ter... |     {'book': 1}     |
      +------------------+---------------------+---------------------+ 
      [2 rows x 3 columns]
      

      【讨论】:

        【解决方案4】:

        您想将每个计数放在单独的列中吗? 在这种情况下,这可能会起作用:

        keywords = ['keyword1' , 'keyword2']
        
        def word_counter(dict_cell , word):
        if word in dict_cell:
            return dict_cell[word]
        else:
            return 0
        
        for words in keywords:
          df[words] = df['word_count'].apply(lambda x:word_counter(x,words))
        

        【讨论】:

          【解决方案5】:
          def count_words(x, w):
              if w in x:
                  return x.count(w)
              else:
                  return 0   
          
          selected_words = ['awesome', 'great', 'fantastic', 'amazing', 'love', 'horrible', 'bad', 'terrible', 'awful', 'wow', 'hate']
          
          for words in selected_words:
              products[words]=products['review'].apply(lambda x:count_words(x,words))
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2015-05-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-09-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多