简体   繁体   中英

Python Dictionary Filtration with items as a list

I have several lists as items in my dictionary. I want to create a dictionary with the same keys, but only with items that correspond to the unique values of the list in the first key. What's the best way to do this? Original:

d = {'s': ['a','a','a','b','b','b','b'],
     'd': ['c1','d2','c3','d4','c5','d6','c7'],
     'g': ['e1','f2','e3','f4','e5','f6','e7']}

Output:

e = {'s': ['a','a','a'],
     'd': ['c1','d2','c3'],
     'g': ['e1','f2','e3']}

f = {'s': ['b','b','b','b'],
     'd': ['d4','c5','d6','c7'],
     'g': ['f4','e5','f6','e7']}

I don't think there is an easy way to do this. I created a (not so) little function for you:

def func(entry):
    PARSING_KEY = "s"
    # check if entry dict is valid (optional)
    assert type(entry)==dict

    for key in entry.keys():
        assert type(entry[key])==list

    first_list = entry[PARSING_KEY]
    first_list_len = len(first_list)
    for key in entry.keys():
        assert len(entry[key]) == first_list_len

    # parsing
    output_list_index = []
    already_check = set()
    for index1, item1 in enumerate(entry[PARSING_KEY]):
        if not item1 in already_check:
            output_list_index.append([])
            for index2, item2 in enumerate(entry[PARSING_KEY][index1:]):
                if item2==item1:
                    output_list_index[-1].append(index2)
            already_check.add(item1)

    # creating lists
    output_list = []
    for indexes in output_list_index:
        new_dict = {}
        for key, value in entry.items():
            new_dict[key] = [value[i] for i in indexes]
        output_list.append(new_dict)

    return output_list

Note that because of the structure of dict, there isn't a "first key" so you have to hardcode the key you want to use to parse (whit the "PARSING_KEY" constant at the top of the function)

original_dict = {
    'a': [1, 3, 5, 8, 4, 2, 1, 2, 7],
    'b': [4, 4, 4, 4, 4, 3],
    'c': [822, 1, 'hello', 'world']
}
distinct_dict = {k: list(set(v)) for k, v in original_dict.items()}
distinct_dict

yields

{'a': [1, 2, 3, 4, 5, 7, 8], 'b': [3, 4], 'c': [1, 'hello', 'world', 822]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM