Python Forum
Trouble with converting list , dict to int values!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with converting list , dict to int values!
#1
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!
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu)
Reply
#3
(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!
Reply
#4
You really need to read some very basic things, e.g. working with dicts
https://python-forum.io/Thread-Basic-Dictionaries
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(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?
Reply
#6
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]
Reply
#7
(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
Reply
#8
hello guys

i found what should i do
i solved it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Removing nan values from a dict tomtom 8 315 Oct-05-2021, 06:44 PM
Last Post: tomtom
  How to pass list of values to a API request URL chetansaip99 0 473 Sep-28-2021, 07:37 AM
Last Post: chetansaip99
  Converting a list to dictinary tester_V 8 645 Jul-02-2021, 09:04 PM
Last Post: tester_V
  Python dictionary with values as list to CSV Sritej26 4 914 Mar-27-2021, 05:53 PM
Last Post: Sritej26
  Converting tkinter listbox into list BigSwiggy 6 1,045 Feb-07-2021, 02:01 PM
Last Post: BigSwiggy
  What type of *data* is the name of a list/tuple/dict, etc? alloydog 9 1,269 Jan-30-2021, 07:11 AM
Last Post: alloydog
  How to discard list repeat values akanowhere 7 1,138 Dec-28-2020, 09:14 PM
Last Post: akanowhere
Question dict value, how to change type from int to list? swissjoker 3 958 Dec-09-2020, 09:50 AM
Last Post: perfringo
  list trouble rediska 3 872 Oct-17-2020, 11:17 AM
Last Post: ibreeden
  Appending list Trouble Big Time Milfredo 7 1,118 Oct-01-2020, 02:59 AM
Last Post: Milfredo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020