to sum a list in a dictonary

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Nov 1 19:35:56 EDT 2007


En Thu, 01 Nov 2007 12:10:55 -0300, Beema shafreen  
<beema.shafreen at gmail.com> escribió:

> dd = {}
> dd2 ={}
> probes = list(enumerate((i.split('\t')[2],i.split('\t')[3],
> i.split('\t')[4])for
> i in open('final_lenght_probe_span')))

Ouch... Can you actually understand what goes on the above line? It's too  
hard for me. Let's rewrite it in a more readable way:

for line in open('final_lenght_probe_span'):
     probe_id, span, length = line.split('\t')[2:5]
     span = float(span)
     length = float(length)
     probes.append((probe_id, span, length))

> 2)when i donot assign NONE
> TypeError: unsupported operand type(s) for +: 'int' and 'str'

Next time post the entire traceback: it says *where* the error occured and  
the full chain of calls. This has a lot of value when you try to figure  
out what happens. In this case, I'll have to use my crystall ball.
sum(item) requires numeric values; spam and length are strings, because  
that's what you read from the file. You have to convert the values into  
numbers using float(), as above.
Move the enumerate onto the following "for" statement

> for idx, (probe_id, span, length) in probes:

for idx, (probe_id, span, length) in enumerate(probes):

>          try :
>                 dd[probe_id] = [span,length.strip(),probes[idx+1][1][1]]
>          except IndexError :
>                 None = 0( is this a right way)
>                 dd[probe_id] = [span,length,None]

if idx+1<len(probes):
     dd[probe_id] = span + length + probes[idx+1][1] # span of next probe?
else:
     dd[probe_id] = span + length

> 1)the error shown is
> when i assign NONE =0
> None = 0
> SyntaxError: assignment to None

Don't assign *to* the name None. None is a unique object, used everywhere  
in Python to denote "nothing", "no value", "no object", "unknown" or  
similar meanings. There exists exactly only one instance of the None  
object. Assigning anything to None is an error.
In this case, and based on the later sum(), I think you want to use the  
value 0.

-- 
Gabriel Genellina




More information about the Python-list mailing list