[Tutor] How to create a dictionary for ount elements

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Wed Jun 4 21:02:16 CEST 2014


On 04.06.2014 12:29, jarod_v6 at libero.it wrote:
> Dear all thanks for your suggestion!!!
>
> In [4]: with open("prova.csv") as p:
>      for i in p:
>          lines =i.rstrip("\n").split("\t")
>          line = (lines[0],lines[1])
>     ...:         diz.setdefault(line,set()).add(lines[2])
>     ...:
>
> In [5]: diz
> Out[5]:
> {('program1', 'sample1'): {'TP53'},
>   ('program1', 'sample2'): {'ATF3', 'PRNP'},
>   ('program2', 'sample1'): {'PRNP', 'TP53'},
>   ('program2', 'sample2'): {'TLK1', 'TRIM32'},
>   ('programs ', 'sample'): {'gene'}}
>
>
> So what I want to do is to use intersect between the keys recursively:
> s = diz[('program2', 'sample1']
>     ....:
>     ....:
> KeyboardInterrupt
>
> In [14]: s = diz[('program2', 'sample1')]
>
> In [15]: s
> Out[15]: {'PRNP', 'TP53'}
>
> In [16]: a
> Out[16]: {'ATF3', 'PRNP'}
>
> In [17]: s.inte
> s.intersection         s.intersection_update
>
> In [17]: s.intersection(a)
> Out[17]: {'PRNP'}
>
> How can Have a intersect of all my dictionary and from ('program1', 'sample1')
> vs ('program1', 'sample2')...
> I want to count  how many genes are common
> Thanks in advance  for your help!
>

So you know the basic method set.intersection (or its operator form &).
The rest depends on which intersections you're interested in.
For all pair-wise comparisons, you can use nested for loops across all 
key, value pairs, like so:
for k1, v1 in your_dict.iteritems():
     for k2, v2 in your_dict.iteritems():
         do_something()

For any kind of systematic comparison, it's probably better to build a 
set of all programs and one of all samples during file parsing, in 
addition to the dict, then use their values for accessing the dict.

Best, Wolfgang


More information about the Tutor mailing list