Python Construction Of Value Set Dictionary
.I've been dealing with list comprehension recently, I came across a problem I can't seem to solve> let's say I have pairs in the form of: A,B,C,X='ABCX' init = {(A,B),(B,C),
Solution 1:
Here is the solution without dict comp
init = {('A','B'),('B','C'),('C','X')}
d = {}
for k, v ininit:
d.setdefault(k, set()).add(v)
d.setdefault(v, set()).add(k)
The problem is that with your current format for the data you can't properly specify the key, values. Ideally it should be.
init = {('A','B'),('B','A'),('B','C'),('C','B'),('C','X'),('X','C')}
Which you can obtain by doing the following if you don't want to / can't adjust your current method for getting the pairs.
init2 = {(y, x) for x, y ininit}.union(init)
So you can then do
d = { key : { v for k, v in init2 if k == key } for key, _ in init2 }
There is also this, but it doesn't include X, and again it would probably become much larger to make work due to the current format.
d = { k : {v1 if k == v2 else v for v1, v2 ininit } for k, v ininit }
Post a Comment for "Python Construction Of Value Set Dictionary"