Dictionary of dictionaries in Python?

后端 未结 3 605
日久生厌
日久生厌 2020-12-28 17:45

From another function, I have tuples like this (\'falseName\', \'realName\', positionOfMistake), eg. (\'Milter\', \'Miller\', 4). I need to write a

相关标签:
3条回答
  • 2020-12-28 18:21

    Using collections.defaultdict is a big time-saver when you're building dicts and don't know beforehand which keys you're going to have.

    Here it's used twice: for the resulting dict, and for each of the values in the dict.

    import collections
    
    def aggregate_names(errors):
        result = collections.defaultdict(lambda: collections.defaultdict(list))
        for real_name, false_name, location in errors:
            result[real_name][false_name].append(location)
        return result
    

    Combining this with your code:

    dictionary = aggregate_names(previousFunction(string))
    

    Or to test:

    EXAMPLES = [
        ('Fred', 'Frad', 123),
        ('Jim', 'Jam', 100),
        ('Fred', 'Frod', 200),
        ('Fred', 'Frad', 300)]
    print aggregate_names(EXAMPLES)
    
    0 讨论(0)
  • 2020-12-28 18:33

    If it is only to add a new tuple and you are sure that there are no collisions in the inner dictionary, you can do this:

    def addNameToDictionary(d, tup):
        if tup[0] not in d:
            d[tup[0]] = {}
        d[tup[0]][tup[1]] = [tup[2]]
    
    0 讨论(0)
  • 2020-12-28 18:34

    dictionary's setdefault is a good way to update an existing dict entry if it's there, or create a new one if it's not all in one go:

    Looping style:

    # This is our sample data
    data = [("Milter", "Miller", 4), ("Milter", "Miler", 4), ("Milter", "Malter", 2)]
    
    # dictionary we want for the result
    dictionary = {}
    
    # loop that makes it work
    for realName, falseName, position in data:
        dictionary.setdefault(realName, {})[falseName] = position
    

    dictionary now equals:

    {'Milter': {'Malter': 2, 'Miler': 4, 'Miller': 4}}
    
    0 讨论(0)
提交回复
热议问题