Skip this part it's not needed but its a precursor to the movie
From my list, I get every possible combination by doing the following
list = {
"list1": [
{"name", "height", 10000000000},
{"name1", "height2", 90000000000}
],
"list2": [
{"name", "height", 30000000000},
{"name1", "height2", 40000000000}
],
"list3": [
{"name", "height", 50000000000},
{"name1", "height2", 60000000000}
]
}
keys = list .keys()
values = (list [key] for key in keys)
combinations = [dict(zip(keys, combination)) for combination in itertools.product(*values)]
This gives me
print(combinations)
[{'list1': {10000000000, 'height', 'name'}, 'list2': {30000000000, 'height', 'name'}, 'list3': {50000000000, 'height', 'name'}}, {'list1': {10000000000, 'height', 'name'}, 'list2': {30000000000, 'height', 'name'}, 'list3': {60000000000, 'name1', 'height2'}}, {'list1': {10000000000, 'height', 'name'}, 'list2': {40000000000, 'name1', 'height2'}, 'list3': {50000000000, 'height', 'name'}}, {'list1': {10000000000, 'height', 'name'}, 'list2': {40000000000, 'name1', 'height2'}, 'list3': {60000000000, 'name1', 'height2'}}, {'list1': {90000000000, 'name1', 'height2'}, 'list2': {30000000000, 'height', 'name'}, 'list3': {50000000000, 'height', 'name'}}, {'list1': {90000000000, 'name1', 'height2'}, 'list2': {30000000000, 'height', 'name'}, 'list3': {60000000000, 'name1', 'height2'}}, {'list1': {90000000000, 'name1', 'height2'}, 'list2': {40000000000, 'name1', 'height2'}, 'list3': {50000000000, 'height', 'name'}}, {'list1': {90000000000, 'name1', 'height2'}, 'list2': {40000000000, 'name1', 'height2'}, 'list3': {60000000000, 'name1', 'height2'}}]
READ FROM HERE
What I now need is to take the combinations
array and order them based on the sum of each combinations dictionarys values at the value with the integer
Assuming we have the first 3 combos for brevity: (removed the extra zeros)
{'list1': {1, 'height', 'name'}, 'list2': {3, 'height', 'name'}, 'list3': {5, 'height', 'name'}},
{'list1': {1, 'height', 'name'}, 'list2': {3, 'height', 'name'}, 'list3': {6, 'name', 'height'}},
{'list1': {1, 'height', 'name'}, 'list2': {4, , 'height', 'name'}, 'list3': {5, 'height', 'name'}}
I want to order them like this: The following takes the sum of each of the field that has integers The order will be:
10
10
9
resulting in: (goal output)
{'list1': {1, 'height', 'name'}, 'list2': {3, 'height', 'name'}, 'list3': {6, 'height', 'name'}},
{'list1': {1, 'height', 'name'}, 'list2': {4, 'height', 'name'}, 'list3': {5, 'height', 'name'}}
{'list1': {1, 'height', 'name'}, 'list2': {3, 'height', 'name'}, 'list3': {5, 'height', 'name'}},
How can I accomplish this?
As a bonus issue, if I were to add another value to dict like:
...
"list1": [
{"name", "height", 10000000000, true},
{"name1", "height2", 90000000000, true}
], ...
How could i also order it by the sum of the value-->[3] and also the boolean. In case the numbers are equal like in the example above
You could define a function that sums all int
s in all values of a dict an use that as sort key?
from itertools import chain
def sum_keys_ints(d):
return sum(i for i in chain(*d.values()) if isinstance(i, int))
combinations.sort(key=sum_keys_ints, reverse=True)
I can't believe it took me this long but i woke up in the middle of the night realizing I was an idiot and figured it out in 20 seconds and went back to sleep
I ended up naming the values as well:
sorted(combinations, key=lambda x: sum(i['value_name'] for i in x.values()), reverse=True)
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.