Understanding Operators for Multiple Variables from which json Extracts Values

Asked 2 years ago, Updated 2 years ago, 20 views

Suppose json takes out the list and contains multiple values in the variable.

A = [1, 2, 3, 4, 5]

B = [-1, -2, -3, -4, -5]

I want to do A+B at this time, but it doesn't work

Replace AB with the value of 1 of A and -1 of B plus
Replace AB with the value of -2 of A and -2 of B. Replace AB with the value of -3 of A and -3 of B. Replace AB with the value of -4 of A and -4 of B. Replace AB with the value of -5 of A and -5 of B.

I'd like to move.I want AB to be a variable with 5 values in the end.

 A = [1,2,3,4,5]
B = [-1, -2, -3, -4, -5]

A + B = AB
print(AB)

Error Code

SyntaxError: can't assign to operator

python

2022-09-30 20:10

2 Answers

SyntaxError: can't assign to operator: Syntax error. Operator cannot assign!

The operator here is +, and in short, A+B=AB is interpreted as substituting AB for A+B, which is impossible. If you want to define AB as A+B, write it like this

AB=A+B

(By the way, I recommend that you review the basics of grammar rather than introductory books so far.)

As you can see when you try, this is what AB looks like in the above writing.

 [1, 2, 3, 4, 5, -1, -2, -3, -4, -5]

This is because list, + attaches the two lists together. Defined. (Numpy changes the situation, but) I usually write element-ref=.As you say by https://ja.stackoverflow.com/users/16894/metropolis">@metropolis

AB=map(sum,zip(A,B))

Or

from operator import add
AB = map(add, A, B)

I think it's around.

(For things with multiple elements such as lists and matrices, calculating each element is called element-wise. On the contrary, A+B is an operation as a whole list, and matrix product is treated the same.)


2022-09-30 20:10

If you want Python to handle data, study Pandas and NumPy.It can perform vector operations and has the ability to handle a lot of data.This issue can also be briefly described as follows:

import numpy as np

a = np.array ([1, 2, 3, 4, 5])
b = np.array ([-1, -2, -3, -4, -5])
ab = a + b
print(ab)


2022-09-30 20:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.