googledatalab / notebooks

Google Cloud Datalab samples and documentation
Apache License 2.0
341 stars 161 forks source link

AttributeError: 'dict_values' has no attribute 'index' #154

Open demetrii opened 6 years ago

demetrii commented 6 years ago

Upon running for s in sources: source, predicted = predictor.predict(s) print('\n---SOURCE----\n' + source) print('---PREDICTED----\n' + predicted)

I get the following error: INFO:tensorflow:Restoring parameters from /content/lab/datalab/punctuation/model/punctuation-5796

AttributeError Traceback (most recent call last)

in () 9 10 for s in sources: ---> 11 source, predicted = predictor.predict(s) 12 print('\n---SOURCE----\n' + source) 13 print('---PREDICTED----\n' + predicted) in predict(self, content) 88 for i in indices: 89 words1[i], words1[i-1] = words1[i-1], words1[i] ---> 90 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))] 91 all_words = words1 + [puncts[-1]] + words2 92 content = ' '.join(all_words) in (.0) 88 for i in indices: 89 words1[i], words1[i-1] = words1[i-1], words1[i] ---> 90 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))] 91 all_words = words1 + [puncts[-1]] + words2 92 content = ' '.join(all_words) AttributeError: 'dict_values' object has no attribute 'index' This is how index is defined: ` 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] ` #only line in for loop ` 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) ` Can anyone explain why this occurs and/or how to fix this?
demetrii commented 6 years ago

This error is because the .index() method is supported by Python 2, but unsupported by Python 3. I am currently trying to find a way around. Feel free to share (potential) solutions

demetrii commented 6 years ago

Rewrote: [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))] to the following: [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))] However, the error that I get is 'dict_keys' object does not support indexing. Any suggested solution?

shubham8garg commented 6 years ago

Did you try this:- [list(self._word_to_id.keys())[list(self._word_to_id.values()).index(data_x[index])]

I guess this can unblock you.