Python 初始化字典,循环列表,在字典中添加单词和递增计数器

Python 初始化字典,循环列表,在字典中添加单词和递增计数器,python,loops,dictionary,Python,Loops,Dictionary,我添加了一个示例,希望能让这一点更清楚。我正在尝试做以下工作:将单词dict中没有的单词从句子中添加到列表中,然后将这些单词添加到字典“新词dict”中。只添加一次单词,不重复,并增加计数器以跟踪单词出现的次数。例如,“happy”在字典中出现一次,计数器为2 我可以创建列表并将单词添加到字典中,但我没有正确地增加字典中的值 words = {'abandon':-2,'abandoned':-2,'abandons':-2,'abducted':-2,'abduction':-2, 'abdu

我添加了一个示例,希望能让这一点更清楚。我正在尝试做以下工作:将单词dict中没有的单词从句子中添加到列表中,然后将这些单词添加到字典“新词dict”中。只添加一次单词,不重复,并增加计数器以跟踪单词出现的次数。例如,“happy”在字典中出现一次,计数器为2

我可以创建列表并将单词添加到字典中,但我没有正确地增加字典中的值

words = {'abandon':-2,'abandoned':-2,'abandons':-2,'abducted':-2,'abduction':-2,
'abductions':-2,'abhor': -3}
sentence = {'abandon', 'abandoned', 'abhorrent', 'hello', 'smart', 'hello', 'die',
'happy', 'sad', 'up', 'down', 'happy', 'smart','cool', 'clean', 'mean'}         

new_words_list = []                  #Empty list
new_words_dict = {}                  #Empty dict 

for word in sentence:
if word not in words:    
        new_words_list.append(word)  
print new_words_list        

for word in new_words_list:
    if word not in new_words_dict:
        new_words_dict[word] = {}
        new_words_dict[word] =1
else:
    if word in new_words_dict:
        new_words_dict[word] +=1              
print new_words_dict

这会打印新词,但值计数器的增量不正确。

您这样做的方式令人困惑且不必要地复杂。可以为您完成大部分工作:

words = {'abandon':-2,'abandoned':-2,'abandons':-2,'abducted':-2,'abduction':-2,
'abductions':-2,'abhor': -3}
sentence = {'abandon', 'abandoned', 'abhorrent', 'hello', 'smart', 'hello', 'die',
'happy', 'sad', 'up', 'down', 'happy', 'smart','cool', 'clean', 'mean'}         

new_words_list = []                  #Empty list
new_words_dict = {}                  #Empty dict 

for word in sentence:
if word not in words:    
        new_words_list.append(word)  
print new_words_list        

for word in new_words_list:
    if word not in new_words_dict:
        new_words_dict[word] = {}
        new_words_dict[word] =1
else:
    if word in new_words_dict:
        new_words_dict[word] +=1              
print new_words_dict
import collections

words = {'abandon':-2,'abandoned':-2,'abandons':-2,'abducted':-2,'abduction':-2,
'abductions':-2,'abhor': -3}
# throw away the values in the dictionary because they aren't being used
words = words.keys()  

# this is a list [] not a dictionary ()
sentence = ['abandon', 'abandoned', 'abhorrent', 'hello', 'smart', 'hello', 
    'die', 'happy', 'sad', 'up', 'down', 'happy', 'smart','cool',
    'clean', 'mean'] 


counts = collections.Counter([word for word in sentence if word not in words])

print counts 
Counter({'hello': 2, 'smart': 2, 'happy': 2, 'down': 1, 'die': 1, 'up': 1, 
'sad':1, 'abhorrent': 1, 'clean': 1, 'mean': 1, 'cool': 1})

这可能不是您真正想要的,因为您的代码已被破坏。据我所知,它确实满足了您的问题要求。

我同意msw,您的代码太复杂了。尽管如此,由于您希望坚持使用它并从中学习一些东西,下面是您的代码,其中包含一些注释和更正:

# dictionary with weird values, but we're only counting keys anyway
words = {'abandon':-2,'abandoned':-2,'abandons':-2,'abducted':-2,'abduction':-2,
'abductions':-2,'abhor': -3}
# this is a list, not a dictionary!
sentence = ['abandon', 'abandoned', 'abhorrent', 'hello', 'smart', 'hello', 'die',
'happy', 'sad', 'up', 'down', 'happy', 'smart','cool', 'clean', 'mean']

# empty list to save words in
new_words_list = []
# empty dict to save words and counts in
new_words_dict = {}

# first loop finds new words and saves them in new_words_list
for word in sentence:
  if word not in words:    
    new_words_list.append(word)  
print new_words_list        

# second loop counts new words
for word in new_words_list:

  # if the word is not in the dictionary yet, add it as key with another
  # dictionary as value (which doesn't make sense). Then replace this empty
  # dictionary with a simple integer (1).
  if word not in new_words_dict:
    new_words_dict[word] = {}
    new_words_dict[word] = 1

  # this should be elif, I assume. Then it would increment the counter of the
  # word in new_words_dict by 1.
  elif word in new_words_dict:
    new_words_dict[word] += 1              
print new_words_dict
请注意,第二个循环中的
新词dict[word]={}
没有任何意义,因为您需要的是计数器而不是字典


顺便说一下,我看这里的柜台没有问题。这可能是因为我更正了您的
else
/
elif
语句的缩进(如果您没有在此处缩进,或者也没有在原始代码中缩进,我就不是苏了。)

我看到您对每个
都做了几次
。但是,您不应该在某个地方使用这些
值吗?还有,
tweetscore
在哪里定义?这是一个极不完整的代码。@Keving。同意,但是当我将它合并到循环中时,我得到了一个错误,比如:new_terms[each][“neg_tweets”]+=1。这是我得到的结论:{0:{},'neg_tweets':0,'total_tweets':4,'pos_tweets':0,u'online':{}。在线这个词我很难从词典中提取出来。你为什么要为每个词创建一本新词典呢?你认为你的代码的第二行(new_terms[term]={})在做什么?我在这里的意图是:(new_terms[term]={})在dict中添加一个新词,但我认为我在这里出错了。我正在浏览一个列表,并向dict中添加唯一的单词,同时递增计数器。dict输出应该是新的{term,[pos_tweets,neg_tweets,total_tweets]}基于这个问题,这是正确的。谢谢问题:我几乎可以在上面的代码中得到他的结果,但计数器并没有正确地递增,有什么线索吗?我现在更愿意坚持这种结构,谢谢。我甚至不能理解你代码的大致结构。例如,
if word not in words:new_words_list=[]
在每个循环中清空
new_words_list
,以便
new_words_list
中只能有一个元素。如果你的意思是我的代码计数器不正确,我在
句子中数2个“hello”,1个“aborarent”,以此类推。理解,更新示例。你的解决方案奏效了,但我想坚持我现有的结构。这不是很优雅,但对我来说是很好的学习锻炼。所以,我的目标是将列表项作为键添加到字典中,并为列表中的每个事件增加一个计数器。我快到了,但是计数器没有正确地递增。那就是我希望得到帮助的地方。你看到我的答案了吗?也许这只是一个缩进问题?