-1

I want to compare some values using "if" in the code below, but it doesn't work:

if Slice_num[person][i, [1]] == Z_pos:   # Slice_num[0][15, [1]] is ['-10.000000'] and Z_pos = -10.000000 
    absname = os.path.join(root, dcmfile)

Example: Values in the above variables are equal:

Z_pos: -10.000000 , <class 'pydicom.valuerep.DSfloat'>
Slice_num[person][i, [1]]: ['-10.000000'] , <class 'numpy.ndarray'>

So, when the code is performed, ifstatement cannot be applied and the next line after ifstatement cannot be run. while the value (-10.000000) is same in both of them. How can the problem be solved, so that ifstatement find them equal?

Ellie
  • 303
  • 2
  • 16
  • Please provide the full error trace, and try to give a [mcve] (which may be difficult in the instance as there is some IO involved) so that we can replicate the error and try to help. – Daniel F Oct 16 '18 at 09:36
  • Also, try to only ask one question at a time. Asking two means you may not get any answer as somoene who can answer one part might not be bale to answer the other. – Daniel F Oct 16 '18 at 09:38
  • @Daniel F: The question was changed. – Ellie Oct 16 '18 at 11:15
  • Possible duplicate of [Identical float values comparing as inequal](https://stackoverflow.com/questions/24853323/identical-float-values-comparing-as-inequal) – John Zwinck Oct 16 '18 at 11:20
  • The link couldn't be solved my problem. – Ellie Oct 16 '18 at 11:57

1 Answers1

0

The values are actually not the same. Slice_num[person][i, [1]] is a numpy.ndarray containing one item, namely the value you want to compare. Try
Slice_num[person][i, [1]][0] == Z_pos

g_uint
  • 1,903
  • 3
  • 17
  • 30
  • `Slice_num[person][i, [1]][0] == Z_pos` wasn't worked. `Slice_num[person][i, [1]][0] = {str_} -10.000000` and `Z_pos = {DSfloat} -10.000000`. That is beacause of data structures. – Ellie Oct 16 '18 at 16:59
  • Yes. so trying that suggestion gave you acces to the value you want. Now cast it to whichever type works for you :) – g_uint Oct 17 '18 at 05:58