I would like to get a very uncommon output, as the follows Desired output shows:
I have tried many ways for a whole day, but still, no idea how to get it.
Two lists:
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'SS']
Desired output:
{'11': ['E'],
'22': ['AA','33','44'], # order does not matter
'33': ['AA','22','44'],
'44': ['AA','22','33'],
'55': ['SS','66'],
'66': ['SS','55']}
For better ubderstanding, if a and b are:
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'AA']
Desired output:
{'11': ['E'],
'22': ['AA','33','44','66'], # order does not matter
'33': ['AA','22','44','66'],
'44': ['AA','22','33','66'],
'55': ['SS'],
'66': ['AA','22','33','44']
}
Question: Which is suitable to get my desired output? tuple, dictionary or set?
My current status:
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'SS']
#put them into a dictionary:
dict_1 = {}
for i, j in zip(a, b):
dict_1.setdefault(i, []).append(j)
print(dict_1)
c = list(zip(a,b))
i=0
#find the target tuples
while i<len(c):
j = i+1
while i < j < len(c):
if c[i][1] == c[j][1]:
print('Got a repeat one')
print(c[i], c[j])
j+=1
i+=1
Current output:
{'11': ['E'], '22': ['AA'], '33': ['AA'], '44': ['AA'], '55': ['SS'], '66': ['SS']}
Got a repeat one
('22', 'AA') ('33', 'AA')
Got a repeat one
('22', 'AA') ('44', 'AA')
Got a repeat one
('33', 'AA') ('44', 'AA')
Got a repeat one
('55', 'SS') ('66', 'SS')
Open question:
once got the target tuples, how can I combine them to get my desired output, I have tried using .append, but it is a mass.
Incorrect output while trying to collect targets:
['AA', '33', 'AA', '44', 'AA', '44', 'SS', '66']
['AA', '33', '44', 'SS', '66']
If anyone has a hint on this, I much appreciate it, thanks in advance!