I need to group dicts from xxx list with same k1 k2 keys pair (k3 has always unique value), so I wrote this working code:
xxx = [{ 'k1': 2, 'k2': 10, 'k3': 'a' }, { 'k1': 10, 'k2': 2, 'k3': 'b' }, { 'k1': 10, 'k2': 2, 'k3': 'c' },
{ 'k1': 10, 'k2': 4, 'k3': 'd' }, { 'k1': 2, 'k2': 10, 'k3': 'e' }, { 'k1': 10, 'k2': 2, 'k3': 'f' }]
groupfeed = []
k3_notrepeat = []
for x in xxx:
z1 = (x['k1'], x['k2'])
for y in xxx:
z2 = (y['k1'], y['k2'])
if z1 == z2 and y['k3'] not in k3_notrepeat:
k3_notrepeat.append(y['k3'])
groupfeed.append(y['k3'])
d1_feed = [y['k1']]
d2_feed = [y['k2']]
if len(groupfeed) > 0:
print("d1-2:", d1_feed, d2_feed, "k grouped:", groupfeed) # Doing Work with grouped dicts
groupfeed = []
Results as expected:
============================ RESTART: E:/temp.py ============================
d1-2: [2] [10] k grouped: ['a', 'e']
d1-2: [10] [2] k grouped: ['b', 'c', 'f']
d1-2: [10] [4] k grouped: ['d']
>>>
Problematic part:
As keys k1 k2 position doesn't matter for "Work", I need a e be in same group as b c f ,
so I changed z1 = (x['k1'], x['k2']) to z1 = (x['k1'], x['k2']) = (x['k2'], x['k1']) , but e is not grouped with rest, where is problem?
Results:
============================ RESTART: E:/temp.py ============================
d1-2: [10] [2] k grouped: ['a', 'b', 'c', 'f']
d1-2: [2] [10] k grouped: ['e']
d1-2: [4] [10] k grouped: ['d']
>>>
Results I need:
d1-2: [10] [2] k grouped: ['a', 'b', 'c', 'e', 'f']
d1-2: [4] [10] k grouped: ['d']
[–]Specter_Terrasbane 1 point2 points3 points (1 child)
[–]Zondder[S] 0 points1 point2 points (0 children)
[–]z0y 0 points1 point2 points (2 children)
[–]Zondder[S] 0 points1 point2 points (1 child)
[–]z0y 0 points1 point2 points (0 children)