Creating a dictionary for a value in a dictionary in Python

I am trying to add dictionaries that have a key to each element from the list and values ​​from one next element from the list and a number for the number of times that it follows it in the dictionary format. For example, if we have a list of words, ['The', 'cat', 'chased', 'the', 'dog'] , and if the key is "the", I want the values ​​to be {'dog: 1, 'cat: 1}. The whole output should be {'the': {'dog': 1, 'cat': 1}, 'chased': {'the': 1}, 'cat': {'chased': 1}} .

My code can still generate key and values, but not in a dictionary in dictionary format. Can anyone help with this?

My code is:

 line = ['The', 'cat', 'chased', 'the', 'dog'] output = {} for i, item in enumerate(line): print(i, item, len(line)) if i != len(line) - 1: output[item] = line[i+1]=i print(output) 

Output:

 {'The': 'cat', 'chased': 'the', 'the': 'dog', 'cat': 'chased'} 
+6
source share
3 answers

You can use collections.Counter for this. Example -

 line = ['The', 'cat', 'chased', 'the', 'dog','the','dog'] from collections import Counter output = {} for i, item in enumerate(line): print(i, item, len(line)) if i != len(line) - 1: output.setdefault(item.lower(),Counter()).update(Counter({line[i+1]:1})) print(output) 

.setdefault() first checks to see if a key exists if it does not set it in the second argument, and then returns a value on that key.

In Counter, when you do .update() , if the key already exists, it increments the count by 1, so this seems like the correct structure used for your case.

In addition, Counter behaves just like a regular dictionary, so you can use them just like any dictionary.


Demo (note the modified input to show the scenario in which 'dog' followed 'the' twice) -

 >>> line = ['The', 'cat', 'chased', 'the', 'dog','the','dog'] >>> from collections import Counter >>> output = {} >>> for i, item in enumerate(line): ... print(i, item, len(line)) ... if i != len(line) - 1: ... output.setdefault(item.lower(),Counter()).update(Counter({line[i+1]:1})) ... 0 The 7 1 cat 7 2 chased 7 3 the 7 4 dog 7 5 the 7 6 dog 7 >>> print(output) {'dog': Counter({'the': 1}), 'cat': Counter({'chased': 1}), 'chased': Counter({'the': 1}), 'the': Counter({'dog': 2, 'cat': 1})} 
+2
source

I have not tested this, but something like this maybe? Using defaultdict :

 from collections import defaultdict line = ['The', 'cat', 'chased', 'the', 'dog'] output = defaultdict(lambda: defaultdict(int)) for t, token in enumerate(line[:-1]): output[token.lower()][line[t + 1].lower()] += 1 
+4
source

It looks like the line that is causing you problems is the line:

 output[item] = line[i+1]=i 

It seems you do not take into account that the output of [item] should point to a dictionary. It sounds like you had to make amends for the words to compare correctly.

I was able to get the desired result using the following code:

 line = ['The', 'cat', 'chased', 'the', 'dog'] output = {} length = len(line) # I didn't wanted to check this each iteration for i, item in enumerate(line): item = item.lower() if i != length - 1: next_word = line[i + 1].lower() if item in output: output[item][next_word] = 1 else: output[item] = {next_word: 1} print(output) 
0
source

All Articles