简体   繁体   中英

“for unit, object in enumerate” not working as I think it should

Could someone please give me an idea how to get round this little code problem I'm having.

My bit of code:

dictionary = {}      
word_pos_list = []

for unit, object in enumerate(words_list, start = 1):

  if object in dictionary:  
      word_pos_list.append(dictionary[object])    

  else:                                   
      dictionary[object] = unit     
      word_pos_list.append(unit) 

Here is the problem I am having.

Take this as an example list of words for the variable 'words_list': ['this', 'sentence', 'is', 'a', 'very', 'very', 'good', 'sentence']

The result I would end up with would be: [1, 2, 3, 4, 5, 5, 7, 2]

When a word is found again in the sentence it's value from the dictionary is being displayed correctly as shown with the word 'very' (No. 5) but I'm losing the next 'unit' value, in this example it was No. 6, as you can see the next unique word in the sentence ends up being 7.

What can I do to stop this happening? Thanks in advance for your time and help.

It seems like you are not really looking for the position of the word in the sentence, that enumerate gives you, but how many different words you have seen so far. For this, you can just check the number of entries that are currently in the dictionary.

dictionary = {}
word_pos_list = []
for word in sentence:   
    if word not in dictionary:
        dictionary[word] = len(dictionary) + 1
    word_pos_list.append(dictionary[word])

For your sentence, word_pos_list will then be [1, 2, 3, 4, 5, 5, 6, 2]

As mentioned in one of the comments, there doesn't seem to be a really good reason to be using enumerate here. It's a little cleaner to manually count the items.

words_list = ['this', 'sentence', 'is', 'a', 'very', 'very', 'good', 'sentence']

dictionary = {}      
word_pos_list = []

counter = 0
for word in words_list:
    if word not in dictionary:
        counter += 1
        dictionary[word] = counter

    word_pos_list.append(dictionary[word])

print word_pos_list # [1, 2, 3, 4, 5, 5, 6, 2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM