Python Forum
Trouble with converting list , dict to int values! - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Trouble with converting list , dict to int values! (/thread-29457.html)



Trouble with converting list , dict to int values! - faryad13 - Sep-03-2020

Hello guys
good day

I have a list of 4 points with x values:
[{'X': 2593.75}, {'X': 2457.42}, {'X': 2593.75}, {'X': 2457.42}] <class 'list'>

for example x(1) is:
{'X': 2457.42} <class 'dict'>

Now I need to calculate for example difference between x(1) and x(2) values:
diff = abs (x(1)-(2))

but i receive an error:
unsupported operand type(s) for -: 'dict' and 'int'

Would you please help me how to fix this problem? How can I get X integer values?
Thanks in advance for your help!


RE: Trouble with converting list , dict to int values! - DPaul - Sep-03-2020

It is better to show us the code that fails.
But as a general observation, your list should have a name e.g. "L"
Then L[1] is a dictionary, not a value.

Paul


RE: Trouble with converting list , dict to int values! - faryad13 - Sep-03-2020

(Sep-03-2020, 12:06 PM)DPaul Wrote: It is better to show us the code that fails.
But as a general observation, your list should have a name e.g. "L"
Then L(1) is a dictionary, not a value.

Paul

yes L(1) is a dictionary like: L(1)={'X': 2457.42} and L(2)={'X': 2593.75}
I need to calculate values, for example 2457.42 - 2593.75 !
I do not know how to do!


RE: Trouble with converting list , dict to int values! - buran - Sep-03-2020

You really need to read some very basic things, e.g. working with dicts
https://python-forum.io/Thread-Basic-Dictionaries


RE: Trouble with converting list , dict to int values! - faryad13 - Sep-03-2020

(Sep-03-2020, 12:14 PM)buran Wrote: You really need to read some very basic things, e.g. working with dicts
https://python-forum.io/Thread-Basic-Dictionaries

Thanks for your advice, i am a beginner in python
i have a code with output x as a list:
[{'X': 2593.75}, {'X': 2457.42}, {'X': 2593.75}, {'X': 2457.42}] <class 'list'>

i want just to know if it is possible to do mathematical operation on the integer values, for example 2593.75 , ,2457.42 , ....

if yes, how?


RE: Trouble with converting list , dict to int values! - deanhystad - Sep-03-2020

I'm not sure if you could pack in any more errors into such a short line of code.
diff = abs (x(1)-(2))
Start with x is a list. You cannot do x(). You can do x[]. x[1] is a dictionary.
diff = abs (x[1]-(2))
Now (2) is the number 2. So this tries to subtract the number 2 from a dictionary. That is not allowed. I am going to assume you want to subtract the X number in dictionary x[1] from the X number in dictionary x[2]
diff = abs (x[1]- x[2])
This is closer but still wring because it subtracts a dictionary from a dictionary and you want to subtract the 'X' attributes.
diff = abs (x[1].X- x[2].X)
There is probably a reason you are using a list of dictionaries that is not discussed in your post instead of just putting the numbers in the list like this.
x = [2593.75, 2457.42, 2593.75, 2457.42]


RE: Trouble with converting list , dict to int values! - faryad13 - Sep-03-2020

(Sep-03-2020, 12:40 PM)deanhystad Wrote: I'm not sure if you could pack in any more errors into such a short line of code.
diff = abs (x(1)-(2))
Start with x is a list. You cannot do x(). You can do x[]. x[1] is a dictionary.
diff = abs (x[1]-(2))
Now (2) is the number 2. So this tries to subtract the number 2 from a dictionary. That is not allowed. I am going to assume you want to subtract the X number in dictionary x[1] from the X number in dictionary x[2]
diff = abs (x[1]- x[2])
This is closer but still wring because it subtracts a dictionary from a dictionary and you want to subtract the 'X' attributes.
diff = abs (x[1].X- x[2].X)
There is probably a reason you are using a list of dictionaries that is not discussed in your post instead of just putting the numbers in the list like this.
x = [2593.75, 2457.42, 2593.75, 2457.42]

Thanks for your answer.
yes,,,i have a list with dictionaries
[{'X': 2593.75}, {'X': 2457.42}, {'X': 2593.75}, {'X': 2457.42}] <class 'list'>
and i want just to subtract number values
maybe 2593.75 - 2457.42
that is all...i need only the values not dictionaries


RE: Trouble with converting list , dict to int values! - faryad13 - Sep-04-2020

hello guys

i found what should i do
i solved it