0

Given a dict as input, the output should look as below(keys with common elements must be grouped). I have been trying to use groupby to get this done, but, I am not able to figure it out. Can you guys please help? groupby documentation is a bit difficult to understand for me.

input = {'c1': ['f1', 'f2', 'f3'], 
         'c2': ['f2'],
         'c3': ['f4'],
         'c4': ['f5', 'f4'],
         'c5': ['f3'],
         'c6': ['f6']}
output = [['c1', 'c2', 'c5'], ['c3', 'c4'], ['c6']]
  • So your input is a list of dictionaries, each with a single (but different) key? – jedwards Mar 26 '15 at 07:28
  • How do you go from your input to output -- whats the logic there? – jedwards Mar 26 '15 at 07:30
  • I am assuming....some element to be common in dict's value list. e.g `f2` is common for 'c1' and 'c2'. – Barun Sharma Mar 26 '15 at 07:32
  • Can you explain logic behind grouping of keys in output? – vrajs5 Mar 26 '15 at 07:32
  • Is it realted to http://stackoverflow.com/questions/773/how-do-i-use-pythons-itertools-groupby ? – Vladimir T. Mar 26 '15 at 07:55
  • The problem is ill-stated. Without proper statement of what subsets of keys you're looking for it is impossible to solve. E.g. there is no (apparent) constraint on subsets of how many elements you're looking for. There are potentially many more subsets of length 1, 2, ... 6 of keys that have 1 or more values in common. – emvee Mar 26 '15 at 09:10
  • sry guys, I have changed the question a bit, my problem eventually boils down to a dict as input – Kaushik Lingerkar Mar 26 '15 at 09:28

2 Answers2

0

Please check this. Doesn't look like a very clean solution. But works :) I have used sets to check if there is something common in the values of dict. Will check how groupby does this.

input_list = [{'c1': ['f1', 'f2', 'f3']}, {'c2': ['f2']}, {'c3': ['f4']}, {'c4': ['f5', 'f4']}, {'c5': ['f3']}, {'c6': ['f6']} ]

result_list = []

for input_item in input_list:
    input_key, input_value = input_item.items()[0]
    if not result_list:
        result_list.append([[input_key], input_value])
        continue
    for index, result_item in enumerate(result_list):
        result_keys, result_value = result_item
        if set(result_value).intersection(input_value):
            result_keys.append(input_key)
            result_list[index] = [result_keys, list(set(result_value).union(input_value))]
            break
    else:
        result_list.append([[input_key], input_value])

print [result[0] for result in result_list]  #prints [['c1', 'c2', 'c5'], ['c3', 'c4'], ['c6']]
Barun Sharma
  • 1,452
  • 2
  • 15
  • 20
0

You'd better clearly explain the logic between input and out.

input = {'c1': ['f1', 'f2', 'f3'], 
         'c2': ['f2'],
         'c3': ['f4'],
         'c4': ['f5', 'f4'],
         'c5': ['f3'],
         'c6': ['f6']}

result = []
founded = set()
for k,v in input.items():
    res = set()
    res.add(k)

    for l,m in input.items():
        if set(v) - set(m) != set(v):
            founded.add(l)
            res.add(l)
    for r in result:
        if res.issubset(r):
            res = None
            break
    if res: result.append(res)
print result
Tong Liu
  • 46
  • 2