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

2024-09-29 02:27:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我添加了一个例子,希望能让这一点更清楚。我试着做到以下几点:把单词dict中没有的句子中的单词添加到一个列表中,然后将这些单词添加到字典中“new_words_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

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


Tags: in列表newif字典计数器单词dict
2条回答

你在用一种令人困惑和不必要的复杂方式来做这件事。collections.Counter可以为您完成大部分工作:

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

请注意,第二个循环中的new_words_dict[word] = {}没有任何意义,因为您需要的是计数器而不是字典。在

我看不出柜台有什么问题。这可能是因为我更正了您的else/elif语句的缩进(如果您不在这里缩进或者在原始代码中缩进,我就不是sue了?)。在

相关问题 更多 >