简体   繁体   中英

Creating permuations of a list of dictionaries

I would like to take a list of dictionaries and create a new list of permutations in a specific format.

For example, I've got a list below with dictionaries in it. I would like to create all possible lists using these items in a specific format: (eg result=[dict of type A, dict of type B, dict of type B, dict of type C, dict of type C, dict of Type D, dict of type E, dict of type F]) where the two items of type B cannot be equal and the two items of type C cannot be equal. I will be processing a large set of items so any tips on how to process the permutations more efficiently would be helpful.

{'Items':[
{'Name':'name10','Type':'A','measure1':20,'measure2':20},
{'Name':'name20','Type':'A','measure1':25,'measure2':30},
{'Name':'name30','Type':'B','measure1':15,'measure2':40},
{'Name':'name40','Type':'B','measure1':20,'measure2':50},
{'Name':'name50','Type':'B','measure1':25,'measure2':60},
{'Name':'name60','Type':'C','measure1':15,'measure2':70},
{'Name':'name70','Type':'C','measure1':20,'measure2':80},
{'Name':'name80','Type':'C','measure1':25,'measure2':90},
{'Name':'name90','Type':'D','measure1':10,'measure2':30},
{'Name':'name85','Type':'D','measure1':15,'measure2':30},
{'Name':'name75','Type':'E','measure1':53,'measure2':30},
{'Name':'name65','Type':'E','measure1':10,'measure2':30},
{'Name':'name55','Type':'F','measure1':56,'measure2':10},
{'Name':'name45','Type':'F','measure1':10,'measure2':20}
]}

Let's say your dictionary of Items is in the variable x :

x_set = set((i['Name'], i['Type'], i['measure1'], i['measure2']) for i in x['Items'])
items = [{'Name': i[0], 'Type': i[1], 'measure1': i[2], 'measure2': i[3]} for i in sorted(x_set)]
new_x = {'Items': items}

It would have been nice to simply take the set() of dictionaries but that's not possible in Python. The dictionaries had to be unpacked into tuples, and repacked into dictionaries.

To handle large sets of items, consider breaking them into "shards" based on Name or Type.

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