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) = French version for 'kiss'.
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
  Converting column of values into muliple columns of counts highland44 0 205 Feb-01-2024, 12:48 AM
Last Post: highland44
  Copying the order of another list with identical values gohanhango 7 1,062 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,147 Nov-03-2023, 05:35 PM
Last Post: huzzug
  trouble reading string/module from excel as a list popular_dog 0 384 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Comparing List values to get indexes Edward_ 7 1,083 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,513 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,025 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,161 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  Are list/dict comprehensions interpreted really sequentially? anata2047 3 1,413 May-31-2022, 08:43 PM
Last Post: Gribouillis
  dict class override: how access parent values? Andrey 1 1,594 Mar-06-2022, 10:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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