Iterating through a dictionary back in Python

I have a long dictionary of elements and I want to delete any dictionary words that have only a list of 1 element. for instance

    wordDict={'aardvark':['animal','shell'], 'bat':['animal', 'wings'], 
              'computer':['technology'], 'donut':['food','sweet']}

I want to delete the entry "computer", because there is only one element in it. I started by iterating through wordDict and putting each entry in the dictionary on a separate list so that it looks like this:

    wordList=[['animal','shell'],['animal','wings'],['technology'],['food','sweet']]

and then repeat this list backward, checking to see if each item in the list is longer than 1. Backward, because going forward causes the index to change when it is deleted.

So, in wordList ['technology'] is deleted and that is what remains

    wordList=[['animal','shell'],['animal','wings'],['food','sweet']]

The problem is that since wordDict gets significantly larger (100k + words), it takes a lot of time to put wordDict in a list and then iterate over that list, and I want to make it more efficient.

I thought about iterating through the dictionary backwards, checking to see if each entry has multiple words, and then deletes the dictionary entry if it is not. In the end, what needs to be returned is a list, not a dictionary, so the index does not matter at the end, I used them only for sorting.

Is there any way to do this?

+4
source share
5 answers

You can discard items that you don’t want and create a new dictionary with knowledge of the dictionary, for example

>>> {word: items for word, items in wordDict.items() if len(items) > 1}
{'aardvark': ['animal', 'shell'],
 'bat': ['animal', 'wings'],
 'donut': ['food', 'sweet']}

wordDict , items 1. , , .

+2

, thefourtheye , : -

new_dict = {key:value for key,value in d.iteritems() if len(value) > 1} 

- , , .

0

, :

wordList = list(filter(lambda x: len(x) > 1, wordDict.values()))

...

: ( , )

wordList = list(value for value in wordDict.values() if len(value) > 1)

: empty, :

wordList = list(filter(bool, wordDict.values()))

: ( , ):

wordList = list(value for value in wordDict.values() if value)

( dicts ..) - False.

0

, = 1. = 1 , = 1, , "". , , = 1, "". .

, :

def insert(wordDict, key, element, singles):
    wordDict[key] = element
    if len(element) == 1:
        singles.append(key)

, :

def some_operation(key, element, singles):
    # Do something.
    if len(element) == 1:
        singles.append(key)

, = 1:

def delete_singles(wordDict, singles):
    for k in singles:
        wordDict.pop(k)

delete_single(), . , !

0

You can change the length of the list because it repeats, because it can be done intelligently by iterating backwards. But, as you noticed, it is also slow (O (nk), where k is reflected in the number of elements.

You cannot change dict keys during an iteration, because this can lead to the restoration of the internal hash array, which is the basis of the iteration. Instead, you need to make a separate set of keys for iteration.

wordDict={'aardvark':['animal','shell'], 'bat':['animal', 'wings'], 
              'computer':['technology'], 'donut':['food','sweet']}

for key in list(wordDict.keys()):
    if len(wordDict[key]) <= 1:
        del wordDict[key]

print(wordDict)

prints

{'aardvark': ['animal', 'shell'], 'bat': ['animal', 'wings'], 'donut': ['food', 'sweet']}
0
source

All Articles