-2

I have some problem. I want to substract one list from another. For that I use conversion from python array to numpy array. But it failed. For example, wealthRS is the list. I create a copy: wealthRSCopy = wealthRS Then I want to sustract, but it is error (unsuppoerted operand types) Here is the screenshot.enter image description here

2

1 Answer 1

1

Edit with answer:

You initial lists have lists as their elements. These lists are of different length, so casting to NumPy arrays makes arrays of dtype object, ie the elements of your arrays are lists. See here: https://stackoverflow.com/a/33987165/4244912

When subtracting NumPy arrays, it does elementwise subtraction, that is, it subtracts the elements (which in your case are lists) of one array from the respective elements in the other, which is why you are getting the error message you are (ie. subtraction is not supported for type 'list').

Quick example:

In [1]: import numpy as np

In [2]: A=np.array([[1,2],[],[1,2,3,4]])

In [3]: A[0]
Out[3]: [1, 2]

In [4]: A[0].append(3)    #<-- The first element is a list!

In [5]: A
Out[5]: array([[1, 2, 3], [], [1, 2, 3, 4]], dtype=object)    #<-- The first element (a list) has changed.

Here I reproduce your error:

In [35]: B, C = np.array([[1,2],[3]]), np.array([[4,5],[6]])    # Note the differing sizes of the nested lists.

In [36]: C-B
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-f4554df570db> in <module>()
----> 1 C-B

TypeError: unsupported operand type(s) for -: 'list' and 'list'

So you need to make sure that you can sanely cast your lists to arrays by making each list with the initial lists equal length. Then they should be cast to NumPy arrays with dtype of float and act in the way you expect.

Original post

I don't think your code snippet created a copy, and it looks like you are still subtracting lists, not numpy arrays.

If wealthRS is a list, then wealthRSCopy = wealthRS creates what I believe is called a shallow copy: the lists refer to the same elements, so changing one will change the other.

For instance:

In [1]: a = [1,2,3]

In [2]: b = a

In [3]: b[0] = 10    # change the first item in 'b'

In [4]: b
Out[4]: [10, 2, 3]

In [5]: a    # <-- 'a' has changed too!
Out[5]: [10, 2, 3]

One way to create copies which are independent of each other is by using slices.

In [6]: c = a[:]    # <-- slice containing the whole list

In [6]: c[0] = 15

In [7]: a
Out[7]: [10, 2, 3]

In [8]: c
Out[8]: [15, 2, 3]

Edit: For the rest of your question: Could you try this for me?

In [1]: import numpy as np

In [2]: a, b = [[[1]]], [[[3]]]

In [3]: np.array(b) - np.array(a)
Out[3]: array([[[2]]])

I can't figure out why your subtraction isn't working unless the array elements are lists themselves, but I don't know how that could happen.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.