Insert values from maps into another map

Viewed 58

I have a for loop that generates a map with type of error (from a file.txt) and how many times it occurs. This is inside shown as a map. For example: {' Check failed for display ': 8, ...}. The for loop take each txt file and finds how many times each error happends. However, I want to insert the results from each file.txt into a map located outside the for loop so I can find the total number for "check failed for display, and not only get 8 from the firt time i runs and 3 the next time the for loop runs with a different txt file.

I have this code:

totalErrors={}

    for k in sorted(ErrorTypes):
        if (ErrorTypes[k] not in totalErrors):
            totalErrors += ErrorTypes[k]

ErrorTypes is the map generated inside the for loop for each txt file. And totalErrors is the map I want to compare every result. So for "check failes for display I want to get: totalErrors = {'check failed for display', 11 ...}

1 Answers

You need to use k as a key to the new dictionary, instead of using + operator:

totalErrors={}

for k in sorted(ErrorTypes):
    if (ErrorTypes[k] not in totalErrors):
        totalErrors[k] = ErrorTypes[k]
Related