12

I'm trying to rewrite this Python2 code to Python3 accepted syntax. The .index() methods generates the following error:

AttributeError: 'dict_values' object has no attribute 'index'

This is because .index() is no valid syntax in Python3. I've read that a list should be used to work around the problem, but I can't figure out how to do it. Anyone any idea how to work around the problem?

words1 = [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1)]
indices = [i for i, w in enumerate(words1) if w in PUNCTUATIONS]
for i in indices:
    words1[i], words1[i-1] = words1[i-1], words1[i] 
words2 = [self._word_to_id.keys([self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]
all_words = words1 + [puncts[-1]] + words2  
content = ' '.join(all_words)  
min_step = len(puncts)
Wolfgang Plato
  • 151
  • 1
  • 1
  • 4
  • Welcome to SO! Would you be able to provide some *reproducible* (including desired output) code so we can test solutions? Please also read *[mcve]*. – jpp Mar 01 '18 at 14:12
  • `self._word_to_id.values().index(....` -> `list(self._word_to_id.values()).index(....` to get rid of this error. But using `enumerate` here, would make the code much more readable. Also, are you sure it's `range(len(puncts) - 1` (**-1**)? – CristiFati Mar 01 '18 at 14:14
  • https://stackoverflow.com/questions/16228248/python-simplest-way-to-get-list-of-values-from-dict#16228268 – user9423368 Mar 01 '18 at 14:16
  • Tried it but doesn't work: `'dict_keys' object does not support indexing` – Wolfgang Plato Mar 03 '18 at 15:19

2 Answers2

8

You are calling self._word_to_id.values() which returns the class dict_values and not list. dict_values does not inherit from list and does not have the index method because of that.

You need to convert your dictionary values into a list to use the index function. Try this:

list(self._word_to_id.values()).index(data_x[index])
jsmolka
  • 780
  • 6
  • 15
  • The next error occurs upon working with you suggestion: 'dict_keys' object does not support indexing. Code: `[self._word_to_id.keys()[list(self._word_to_id.values()).index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]` – Wolfgang Plato Mar 03 '18 at 11:34
  • @WolfgangPlato , the `self._word_to_id.keys()` is also a dict_keys object. You need to convert it to a list by `list(self._word_to_id.keys())` and then you can index it. I have had this problem as well. – Valentyn Oct 27 '18 at 04:49
2

words1 = [list(self._word_to_id.keys())[list(self._word_to_id.values()).index(data_x[index])] for index in range(len(puncts) - 1)]

Wolfgang Plato
  • 151
  • 1
  • 1
  • 4
  • 2
    Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Mar 08 '18 at 11:43