Here is what I currently have:
from collections import defaultdict
output = [{'MPID': 'A', 'CLIENTNAME': 'AAA'},
{'MPID': 'C', 'CLIENTNAME': 'BBB'},
{'MPID': 'C1', 'CLIENTNAME': 'CCC'},
{'MPID': 'C2', 'CLIENTNAME': 'CCC'},
{'MPID': 'C3', 'CLIENTNAME': 'CCC'}]
d = defaultdict(list)
for item in output:
d[item['CLIENTNAME']].append(item['MPID'])
final = [{'CLIENTNAME': k, 'MPID': v} for k, v in d.items()]
print final
This output merges the MPID values of matching CLIENTNAMES.
Output:
[{'MPID': ['A'], 'CLIENTNAME': 'AAA'},
{'MPID': ['C'], 'CLIENTNAME': 'BBB'},
{'MPID': ['C1', 'C2', 'C3'], 'CLIENTNAME': 'CCC'}]
What I am trying to do now is format a string with all permutations of each MPID, but ONLY if the dictionary contains more than 1 MPID. (in this example, only CCC has more than 1 MPID).
Here is the query I am formatting:
query = '''x = '{}' and y = '{}' union
x = '{}' and y = '{}';'''
This query needs to compare all MPIDS against one another. The desired output would be:
'''x = 'C1' and y = 'C2' union
x = 'C2' and y = 'C1';'''
'''x = 'C2' and y = 'C3' union
x = 'C3' and y = 'C2';'''
'''x = 'C1' and y = 'C3' union
x = 'C3' and y = 'C1';'''
As you can see, X and Y values swap places in the second line of the string.
What would be an efficient way of going about this part?
Thanks.