help - iter & dict

Alistair King alistair.king at helsinki.fi
Fri Aug 4 06:37:35 EDT 2006


taleinat wrote:
>  <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).
>
>
>   
yes i now the key and values are swapped and the solutions work fine, i 
cant let the numerical key be involved in the iteration as in some cases 
there may be an exvalue below 3. the only solution i could come up with 
is to put single quotes round it but unfortunately i couldnt even to that

thanks

a



-- 
Dr. Alistair King
Research Chemist,
Laboratory of Organic Chemistry,
Department of Chemistry,
Faculty of Science
P.O. Box 55 (A.I. Virtasen aukio 1)
FIN-00014 University of Helsinki
Tel. +358 9 191 50429, Mobile +358 (0)50 5279446
Fax +358 9 191 50366 




More information about the Python-list mailing list