help - iter & dict

taleinat taleinat at gmail.com
Fri Aug 4 06:23:31 EDT 2006


 <aking <at> mappi.helsinki.fi> writes:

> CDSitdict = {28.473823598317392: "'2.4869999999999832'", 40.06163037274758:
> "'0.2910000000000002'", 27.756248559438422: "'2.8349999999999964'",
> 33.2299196586726: "'1.1249999999999962'", 29.989685187220061:
> "'1.9139999999999677'", 31.502319473614037: "'1.490999999999983'",
> 28.487341570327612: "'2.480999999999983'", 30.017763818271245:
> "'1.9049999999999681'", 32.943466663842266: "'1.1789999999999943'",
> 30.520103712886584: "'1.7519999999999736'", 31.453205956498341:
> "'1.5029999999999826'", 29.484222697359598: "'2.084999999999968'",
> 28.413513489228706: "'2.5139999999999842'", 28.314852455260802:
> "'2.558999999999986'", 28.652931545003508: "'2.4089999999999803'"}
> 
> heres the error i get after entering Cpcb
> 
> Traceback (most recent call last):
>   File "elementalDS.py", line 156, in ?
>     CDS = findClosest(CDSitdict, Cpcb)
>   File "elementalDS.py", line 142, in findClosest
>     distance = (target - v) ** 2
> TypeError: unsupported operand type(s) for -: 'float' and 'str'
> alicat <at> linux:~/Desktop>

The Python interpreter is being friendly and explaining exactly what the problem
is and where it occured. It's telling you that it can't subtract a string from a
float, in line 142 of your code (in the findClosest function). So the problem is
that 'target' is a float while 'v' is a string.

'v' should be a float as well, but it's a string since the values in your
dictionary are strings instead of numbers. Try removing the quotes around the
values in your dict (both the double-quotes and the single-quotes).





More information about the Python-list mailing list