Collect and put special key and value together in a dictionary

Viewed 45

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!

2 Answers

Try:

a = ["11", "22", "33", "44", "55", "66"]
b = ["E", "AA", "AA", "AA", "SS", "AA"]

tmp = {}
for i, j in zip(a, b):
    tmp.setdefault(j, []).append(i)

out = {}
for k, v in tmp.items():
    for i in v:
        vc = v.copy()
        vc.remove(i)
        vc.append(k)
        out[i] = vc

print(out)

Prints:

{'11': ['E'], 
 '22': ['33', '44', '66', 'AA'], 
 '33': ['22', '44', '66', 'AA'], 
 '44': ['22', '33', '66', 'AA'], 
 '66': ['22', '33', '44', 'AA'], 
 '55': ['SS']}

Here's a possible solution,

# Assumes the lists are equal length

# Input 1
#a = ['11', '22', '33','44', '55', '66']
#b = ['E', 'AA', 'AA','AA', 'SS', 'SS']

# Input 2
a = ['11', '22', '33','44', '55', '66']
b = ['E', 'AA', 'AA','AA', 'SS', 'AA']

# Dictionary with instances of a that 
# correspond to b - assumes all elements
# of a are unique 
a_in_b = {}
for el_a, el_b in zip(a,b):
    if el_b in a_in_b:
        # If already a key there, append
        a_in_b[el_b].add(el_a)
    else:
        # Initialize a new list
        a_in_b[el_b] = set([el_a])

# Check it
#print(a_in_b)

# Now get the final structure
output = {}
for el_a, el_b in zip(a,b):
    output[el_a] = [el_b]
    rest = a_in_b[el_b] - set([el_a])
    if rest:
        # If not empty
        output[el_a] += list(rest)

print(output)

It first creates a dictionary of all elements in a that would correspond to elements in b, then it constructs the desired output. It uses sets, because of the set difference used later, the - operator.

Note the assumptions as highlighted in the code: a and b are the same size, and a entries are unique. If they are not, then you'd need to use a list. Depending on your requirements you would have to change the - approach too.

Also, consider adjusting the names of variables into something that reflects your application better.

Related