简体   繁体   中英

invalid character in identifier in python 3 in a Jupyter notebook, arrow pointing to a negative sign. Can I not have a negative in my list?

I am trying to find the mean of a list. I believe the negative number in the beginning of my list is somehow throwing me for a loop. Please show me the proper way to find the mean of this simple list.

B = [−5,3,12,190,−10]

print(np.mean(B))

#OUTPUT:

File "<ipython-input-35-5b44dec66063>", line 1
    B = [−5,3,12,190,−10]
          ^
SyntaxError: invalid character in identifier

I'm assuming you maybe copy/pasted your code from somewhere. That is not a valid negative sign in Python. Use the key next to the 0 on your keyboard.

You can check to see if you are using the correct value.

Enter in this for the proper hyphen:

a = ord('-')

And this for yours:

b = ord('−')

and output them to see the difference.

I copied and pasted part of your code into python:

c = '−5'

print(ord(c[0]))
print(ord('-'))

Gives:

8722
45

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM