For more information, see our Privacy Statement. Did you try this:- Who "spent four years refusing to accept the validity of the [2016] election"? The Overflow #47: How to lead with clarity and empathy in the remote world, Creating new Help Center documents for Review queues: Project overview, Feature Preview: New Review Suspensions Mod UX. How can I minimise my opponent's chances to make use of a weak square in my position? Why would using an eraser holder be better than using a normal rectangle eraser? However, the error that I get is 'dict_keys' object does not support indexing. Learn more. 89 words1[i], words1[i-1] = words1[i-1], words1[i] [list(self._word_to_id.keys())[list(self._word_to_id.values()).index(data_x[index])]. ---> 11 source, predicted = predictor.predict(s) Your code can easily be passed a. This has always been one of my biggest issues with everything becoming iterators in Py3. I just translated some Python 2 code to Python 3: which is not pretty, but not very bad either. What is the word used to express "investigating someone without their knowledge"? ---> 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))] To subscribe to this RSS feed, copy and paste this URL into your RSS reader. Any suggested solution? Proper way to declare custom exceptions in modern Python? Cascading common emitter and common collector, 90's PC game, similar to "Another World" but in 3D, dark, purple, locked inside a prison. Learn more, We use analytics cookies to understand how you use our websites so we can make them better, e.g. Successfully merging a pull request may close this issue. Is my Homebrew Born-Lycanthrope Race balanced with other playable races? site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. Iterating over the dictionary directly also yields keys, so turning a dictionary into a list results in a list of all the keys: Not a full answer but perhaps a useful hint. To learn more, see our tips on writing great answers. How to deal with a younger coworker who is too reliant on online sources. In Python2, dict.keys() will return a list of key values. Why does a flight from Melbourne to Cape Town need to go via Doha? Have a question about this project? 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], 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))] Does meat (Black Angus) caramelize just with heat? What would you call a person who is willing to give up their life for others? How should I visualize the average of two bars in a bar chart? The result looks like this. How to sort a list of objects based on an attribute of the objects? all_words = words1 + [puncts[-1]] + words2 What do US universities mean when they mention anything above "Calculus" course. your coworkers to find and share information. 92 content = ' '.join(all_words), AttributeError: 'dict_values' object has no attribute 'index', This is how index is defined: Sturdy and "maintenance-free"? [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))] I am currently trying to find a way around. I wrote this just to show the readability. Help finding a story about two stage sentient beings, Printing 1000000 numbers in 1 sec. Why is python ordering my dictionary like so? Sign in Who figured out that different isotopes have different numbers of neutrons? I am trying to make a US Capitals game for my sister, but when I do the following code: What does this mean and what can I do to fix this? 9 91 all_words = words1 + [puncts[-1]] + words2 Until recently, dictionaries were not even ordered in Python, so accessing the first element was arbitrary. Conventional way of appending the keys to a statically defined list and then indexing it for same. Podcast 286: If you could fix any software, what would you change? How to know if an object has an attribute in Python, How to remove an element from a list by index, AttributeError: 'module' object has no attribute. 10 for s in sources: to your account, Upon running for s in sources: source, predicted = predictor.predict(s) print('\n---SOURCE----\n' + source) print('---PREDICTED----\n' + predicted), AttributeError Traceback (most recent call last) At first, I was about to replace it by the monstrous, before I realised this is all much better written as. Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world. 89 words1[i], words1[i-1] = words1[i-1], words1[i] Why are you indexing your dictionary keys by position? Learn more, AttributeError: 'dict_values' has no attribute 'index'. Sign up for a free GitHub account to open an issue and contact its maintainers and the community. For example, why can't they just support indexing on the iterator, without necessarily having a performance loss? For example, why can't they just support indexing on the iterator, without necessarily having a performance loss? How can I break the cycle of taking on more debt to pay the rates for debt I already have? You signed in with another tab or window. Yes, this is more efficient. How were the cities of Milan and Bruges spared by the Black Death? ['foo', 'hello']. content = ' '.join(all_words) your coworkers to find and share information. What could be the outcome of writing negative things about previous university in an application to another university? Although dictionaries are ordered in Python 3.7, relying on that is not pretty. Interesting discussion on thread safety here reagrding the various approaches to solve this problem: @BrandonBradley: generally speaking: relying on certain actions being atomic is a bad idea anyway, as Python is so highly dynamic. Podcast 286: If you could fix any software, what would you change? Help finding a story about two stage sentient beings. Teacher asking my 5 year old daughter to take a boy student to toilet, 90's PC game, similar to "Another World" but in 3D, dark, purple, locked inside a prison. To subscribe to this RSS feed, copy and paste this URL into your RSS reader. We use analytics cookies to understand how you use our websites so we can make them better, e.g. Stack Overflow for Teams is a private, secure spot for you and I wanted "key" & "value" pair of a first dictionary item. @BrandonBradley: thanks for the link. This error is because the .index() method is supported by Python 2, but unsupported by Python 3. Why doesn't a mercury thermometer follow the rules of volume dilatation? We use essential cookies to perform essential website functions, e.g. How can I manage a remote team member who appears to not be working their full hours? For 10.000.000 items I found it to be almost 40.000 times faster. Can anyone explain why this occurs and/or how to fix this? 12 print('\n---SOURCE----\n' + source) Perhaps you don't need to. I'm trying to access a dict_key's element by its index: In Python 3, the dict.keys() method returns a dictionary view object, which acts as a set. What's the verdicts on hub-less circle bicycle wheels? Why do I get AttributeError: 'NoneType' object has no attribute 'something'? I used the following code. in Python. Thanks for contributing an answer to Stack Overflow! You can find more possible solutions here. By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. How can I get dictionary key as variable directly in Python (not by searching from value)? Dismiss Join GitHub today. How would Earth turn into debris drifting through space without everything at its surface being destroyed in the process? site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa. ---> 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))] min_step = len(puncts). 88 for i in indices: By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy. We use optional third-party analytics cookies to understand how you use GitHub.com so we can build better products. Why would using an eraser holder be better than using a normal rectangle eraser? Thanks for contributing an answer to Stack Overflow! 88 for i in indices: they're used to gather information about the pages you visit and how many clicks you need to accomplish a task. Linux file manager similar to Windows File Explorer (directory tree + file list)? they're used to log you in. If it is really the first item you want*, then. Feel free to share (potential) solutions. At work we recently upgraded to pandas 0.20 and I have a list of numbers that I sort using sort (however this is no longer supported and I am getting the above message when I try sort_values). Making statements based on opinion; back them up with references or personal experience. How to pick one key from a dictionary randomly, Unexpected output when printing dictionary keys, How to remove an element from a list by index. Why did Marty McFly need to look up Doc Brown's address in 1955? How to chose the first 3 values of a column in a dataframe - panda. Fastest way to check if a value exists in a list, How to iterate over rows in a DataFrame in Pandas, Julia - combining vectors into the matrix, The usefulness or otherwise of batteries in multiple guitar effects pedals. The Overflow #47: How to lead with clarity and empathy in the remote world, Creating new Help Center documents for Review queues: Project overview, Feature Preview: New Review Suspensions Mod UX. Analytics cookies. Asking for help, clarification, or responding to other answers. It's definitely more efficient, but so many use cases become "unclean" and complicated for no reason. Is the mosquito in amber inspired by a real object? [self._word_to_id.keys()[self._word_to_id.values().index(data_x[index])] for index in range(len(puncts) - 1, len(data_x))]