dictionary comparing int keys and joins their values if two key are within a certain distance

Daiyue Weng daiyueweng at gmail.com
Tue Nov 7 19:38:28 EST 2017


I have a nested dictionary of defaultdict(dict) whose sub dict have int keys
and lists (list of ints) as values,

'A' = {2092: [1573], 2093: [1576, 1575], 2094: [1577], 2095:
[1574]}'B' = {2098: [1], 2099: [2, 3], 2101: [4], 2102: [5]}'C' =
{2001: [6], 2003: [7, 8], 2004: [9], 2005: [10]}

I union two list values if the difference of their sub keys equals to 1 under
the same outer key e.g. A, and put the joined lists into another list. So
this list will look like,

[1573, 1576, 1575, 1577, 1574][1, 2, 3][4, 5][6][7, 8, 9, 10]

here since 2092, 2093, 2094, 2095 are consecutive by 1, their values are
put into a list [1573, 1576, 1575, 1577, 1574].

based on Detecting consecutive integers in a list
<https://stackoverflow.com/questions/2361945/detecting-consecutive-integers-in-a-list>,
a simple solution can be built when the distance between two
neighbouring sub keys is set to 1.

results = []for key, sub_dict in d.items():
    sub_dict_keys = sorted(sub_dict.keys())
    for k, g in groupby(enumerate(sub_dict_keys), lambda ix: ix[0] - ix[1]):
        consecutive_keys = list(map(itemgetter(1), g))
        val_list = []

        for dict_key in consecutive_keys:
            val_list.extend(sub_dict[dict_key])

        results.append(val_list)
print(results)

, however, the code can only work when the difference between two keys is 1,
I am wondering how to make the code account for an arbitrary distance, e.g.
the distance between two consecutive keys are less than or equal to 2 or 3,
... e.g. set the distance to 2,

'A' = {2092: [1573], 2093: [1576, 1575], 2095: [1577], 2097:
[1574]}'B' = {2098: [1], 2099: [2, 3], 2101: [4], 2102: [5]}'C' =
{2001: [6], 2003: [7, 8], 2008: [9], 2009: [10]}

the result list will look like,

[1573, 1576, 1575, 1577, 1574][1, 2, 3, 4, 5][6, 7, 8][9, 10]



More information about the Python-list mailing list