[Tutor] How to create a dictionary for ount elements

Alan Gauld alan.gauld at btinternet.com
Tue Jun 3 19:31:15 CEST 2014

On 03/06/14 17:24, jarod_v6 at libero.it wrote:

> with open("prova.csv") as p:

If processing csv files its usually better to use the csv modulew, it 
can handle many obscure quoting issues etc, especially if the data is 
sourced from, say, Excel.

In particular I recommend using the dict-reader which returns a
list of dictionaries based on your column names. This makes
writing list comprehensions to extract data sets much easier.

> ['programs ', 'sample', 'gene', 'values']
> ['program1', 'sample1', 'TP53', '2']
> ['program1', 'sample1', 'TP53', '3']
> ['program1', 'sample2', 'PRNP', '4']
> ['program1', 'sample2', 'ATF3', '3']
> ['program2', 'sample1', 'TP53', '2']
> ['program2', 'sample1', 'PRNP', '5']
> ['program2', 'sample2', 'TRIM32', '4']
> ['program2', 'sample2', 'TLK1', '4']
> I want to create a dictionary with set data with the names of the genes:
> example:
> dic = {}
> dic['program1-sample1] = set(TP53)

You could do this using the dict reader of csv module like

tp53set = [sample for sample in data if datum['gene'] == 'TP53']

Obviously you can select which sample columns to keep
as you require.

You ask for a set but don't illustrate what that set
would actually contain so I'm not sure what aspects of
the data you are trying to get at...

> So If I have a dictionary like that I can compare two set  I will compare the
> capacity of the programs in function of the gene show.

Sorry, that was just gobbledegook to me. No idea what you mean.

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list