[Tutor] how can I use Set.interaction function on a dictionary
Alan Gauld
alan.gauld at freenet.co.uk
Sun Apr 23 16:43:51 CEST 2006
> 1. I have a dictionary with 4 keys and some repeated
> values for each key.
>
>>>> dida
> {'NM_001033044': [32842023, 32842023, 32842023,
> 32842023, 32842023, 32842023, 32842023, 32842023,
> 32842023, 32842023, 32842023, 32842023, 32843894,
> 32843894, 32843894, 32843894, 32843894, 32843894,
> 32843894, 32843894, 32843894, 32843894, 32844846,
> 32844846, 32844846, 32844846, 32844846, 32844846,
> 32844846, 32844846, 32844846, 32844846, 32845745],
>>>> didi
> {'NM_001033044': [32842023, 32843894, 32844846,
> 32845745], 'NM_002065': [32844846, 32842023, 32845745,
> 32843894], 'NM_015701': [32844783], 'NM_001033056':
> [32843894, 32844846, 32845745, 32842023]}
>
> Question:
>
> 1. How can I get A^B^C^D(^ = intersection) where A, B,
> C and D are keys from dida or didi.
I assume the value of A,B etc are the lists of data purged
of duplicates? And you now want to find the common elements
across all four sets?
> 2. How can I store values generated dynamically. For
> example, here in 'dida' I know there are 4 keys, but
> how can I create 4 lists while running in for loop and
> store the key values in 'dida'.
I'm not sure what the problem is here:
len(dida.keys())
Will give the number of keys, and
dida[key] will give you the list.
But I think you know enough Python for that to be too simple.
Can you elaborate further?
> Do I have to write an class object that will create
> empty lists depending on number of keys a dictionary
> has?
>
> I am loosing my mind because I do not know how to
> store the values for each key in a loop and do set
> operations.
mysets = []
for value in dida.itervalues():
mysets.append(set(value))
Gives you a list of 4 sets.
You can get the intersect of 4 sets by taking the intersects
of two at a time:
result = mysets[0]
for s in mysets[1:]
result = result.intersection(s)
>>>> for m in range(len(didi.keys())):
> ... x = m+1
> ... a = Set(didi[didi.keys()[m]])
Any time you use len() in a for loop you should ask whether
thats really the best way. And if you then use the index to get
the items, ask again.
> I would be happy, if I could store all 4 sets
> (above)into listsor sets A, B, C and D and do
> (A^B)^(C^D).
See my code above which gives you a list of 4 sets.
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list