Edit: TL;DR
What I have:
dict_1 = {'file1': {'word1': 5, 'word2: 3'}, 'file2': {'word1': 12, 'word2: 0'}}
dict_2 = {'file1': {'total_words': 11}, 'file2': {'total_words': 18}}
What I want
concat_dict = {'file1': {'word1': 5, 'word2: 3', 'total_words' : 11}, 'file2': {'word1': 12, 'word2: 0', 'total_words' : 18}}
Which has added a associated value from dict_2
to the common key in dict_1
.
I have a dictionary that is composed of text file names and the word-count of specific words in those files.
Original post
I want to add also a count for the total words in each file (value for each key), in addition to the specific words in each file, how would I do that?
My code:
# dict_one = previously defined
# filter_words = previously defined
out={}
for k, v in dict_one.items():
#create empty list
new = []
#for each filter word
for i in filter_words:
new.extend(re.findall(r"{}".format(i),v) )
out[k] = dict(Counter(new))
# count total number of words
total_dict = {k: len(v) for k,v in dict_one.items()}
Basically, I want to concatenate total_dict{} and out{} dictionaries, as they have the same keys but I'm unsure how.
Also: I haven't gone through this code in a while, but I forgot what this expression does:
new.extend(re.findall(r"{}".format(i),v) )
I know it's extending the list, but im confused by the arguments of re.findall:
r"{}".format(i),v)
what does this mean/do?