Trouble with negative float values in Python Lists
So i'm trying to work with floats as elements in Python lists but I keep getting this error. I tried making each value a string and then converting it to a float when calling the array to print but that doesn't seem to work either
P1 = [45.100000, ‐65.400000]
print(P1[0])
SyntaxError: invalid character in identifier
Attempt #2
P1 = ["45.100000", "‐65.400000"]
print(float(P1[1]))
ValueError: could not convert string to float: '‐65.400000'
I have a feeling the issues have to do with the negative value in front of the 2nd elements (@ index 1)
python list floating-point
add a comment |
So i'm trying to work with floats as elements in Python lists but I keep getting this error. I tried making each value a string and then converting it to a float when calling the array to print but that doesn't seem to work either
P1 = [45.100000, ‐65.400000]
print(P1[0])
SyntaxError: invalid character in identifier
Attempt #2
P1 = ["45.100000", "‐65.400000"]
print(float(P1[1]))
ValueError: could not convert string to float: '‐65.400000'
I have a feeling the issues have to do with the negative value in front of the 2nd elements (@ index 1)
python list floating-point
I jut copied your code into a python interpreter and it's not working either. I replaced the negative symbol with the one off my keyboard ( - ) and it worked. Are you using the correct negative symbol?
– Polymer
Nov 13 '18 at 0:23
add a comment |
So i'm trying to work with floats as elements in Python lists but I keep getting this error. I tried making each value a string and then converting it to a float when calling the array to print but that doesn't seem to work either
P1 = [45.100000, ‐65.400000]
print(P1[0])
SyntaxError: invalid character in identifier
Attempt #2
P1 = ["45.100000", "‐65.400000"]
print(float(P1[1]))
ValueError: could not convert string to float: '‐65.400000'
I have a feeling the issues have to do with the negative value in front of the 2nd elements (@ index 1)
python list floating-point
So i'm trying to work with floats as elements in Python lists but I keep getting this error. I tried making each value a string and then converting it to a float when calling the array to print but that doesn't seem to work either
P1 = [45.100000, ‐65.400000]
print(P1[0])
SyntaxError: invalid character in identifier
Attempt #2
P1 = ["45.100000", "‐65.400000"]
print(float(P1[1]))
ValueError: could not convert string to float: '‐65.400000'
I have a feeling the issues have to do with the negative value in front of the 2nd elements (@ index 1)
python list floating-point
python list floating-point
asked Nov 13 '18 at 0:17
CosmicCat
825
825
I jut copied your code into a python interpreter and it's not working either. I replaced the negative symbol with the one off my keyboard ( - ) and it worked. Are you using the correct negative symbol?
– Polymer
Nov 13 '18 at 0:23
add a comment |
I jut copied your code into a python interpreter and it's not working either. I replaced the negative symbol with the one off my keyboard ( - ) and it worked. Are you using the correct negative symbol?
– Polymer
Nov 13 '18 at 0:23
I jut copied your code into a python interpreter and it's not working either. I replaced the negative symbol with the one off my keyboard ( - ) and it worked. Are you using the correct negative symbol?
– Polymer
Nov 13 '18 at 0:23
I jut copied your code into a python interpreter and it's not working either. I replaced the negative symbol with the one off my keyboard ( - ) and it worked. Are you using the correct negative symbol?
– Polymer
Nov 13 '18 at 0:23
add a comment |
3 Answers
3
active
oldest
votes
I copied your code and ran it, and all I had to do was replace the "-" Seems like you were using a bad character. Try this;
P1 = [45.100000, -65.400000]
1
Yes, that seems to fix the problem -_-
– CosmicCat
Nov 13 '18 at 0:24
add a comment |
There is a problem with the hyphen you are using. If you cut and paste the hyphen in your list p1
, and check the unicode, it gives:
>>> ord('‐')
8208
Whereas the proper negative or subtraction sign should be:
>>> ord('-')
45
Depending on how you got that list, you either have to figure out why that character got included, or re-type it with the proper Hyphen-Minus
add a comment |
This is because your -
is not a minus sign but a hyphen character:
>>> "‐65.400000".encode('utf-8') # copy from your example
b'xe2x80x9065.400000'
>>> "-65.400000".encode('utf-8') # Replace with my minus
b'-65.400000'
xe2x80x90
is a hyphen character, see here: your hyphen is U+2010
and the hyphen-minus is U+002D
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271987%2ftrouble-with-negative-float-values-in-python-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I copied your code and ran it, and all I had to do was replace the "-" Seems like you were using a bad character. Try this;
P1 = [45.100000, -65.400000]
1
Yes, that seems to fix the problem -_-
– CosmicCat
Nov 13 '18 at 0:24
add a comment |
I copied your code and ran it, and all I had to do was replace the "-" Seems like you were using a bad character. Try this;
P1 = [45.100000, -65.400000]
1
Yes, that seems to fix the problem -_-
– CosmicCat
Nov 13 '18 at 0:24
add a comment |
I copied your code and ran it, and all I had to do was replace the "-" Seems like you were using a bad character. Try this;
P1 = [45.100000, -65.400000]
I copied your code and ran it, and all I had to do was replace the "-" Seems like you were using a bad character. Try this;
P1 = [45.100000, -65.400000]
answered Nov 13 '18 at 0:23
user1394
3121313
3121313
1
Yes, that seems to fix the problem -_-
– CosmicCat
Nov 13 '18 at 0:24
add a comment |
1
Yes, that seems to fix the problem -_-
– CosmicCat
Nov 13 '18 at 0:24
1
1
Yes, that seems to fix the problem -_-
– CosmicCat
Nov 13 '18 at 0:24
Yes, that seems to fix the problem -_-
– CosmicCat
Nov 13 '18 at 0:24
add a comment |
There is a problem with the hyphen you are using. If you cut and paste the hyphen in your list p1
, and check the unicode, it gives:
>>> ord('‐')
8208
Whereas the proper negative or subtraction sign should be:
>>> ord('-')
45
Depending on how you got that list, you either have to figure out why that character got included, or re-type it with the proper Hyphen-Minus
add a comment |
There is a problem with the hyphen you are using. If you cut and paste the hyphen in your list p1
, and check the unicode, it gives:
>>> ord('‐')
8208
Whereas the proper negative or subtraction sign should be:
>>> ord('-')
45
Depending on how you got that list, you either have to figure out why that character got included, or re-type it with the proper Hyphen-Minus
add a comment |
There is a problem with the hyphen you are using. If you cut and paste the hyphen in your list p1
, and check the unicode, it gives:
>>> ord('‐')
8208
Whereas the proper negative or subtraction sign should be:
>>> ord('-')
45
Depending on how you got that list, you either have to figure out why that character got included, or re-type it with the proper Hyphen-Minus
There is a problem with the hyphen you are using. If you cut and paste the hyphen in your list p1
, and check the unicode, it gives:
>>> ord('‐')
8208
Whereas the proper negative or subtraction sign should be:
>>> ord('-')
45
Depending on how you got that list, you either have to figure out why that character got included, or re-type it with the proper Hyphen-Minus
answered Nov 13 '18 at 0:23
sacul
29.9k41740
29.9k41740
add a comment |
add a comment |
This is because your -
is not a minus sign but a hyphen character:
>>> "‐65.400000".encode('utf-8') # copy from your example
b'xe2x80x9065.400000'
>>> "-65.400000".encode('utf-8') # Replace with my minus
b'-65.400000'
xe2x80x90
is a hyphen character, see here: your hyphen is U+2010
and the hyphen-minus is U+002D
add a comment |
This is because your -
is not a minus sign but a hyphen character:
>>> "‐65.400000".encode('utf-8') # copy from your example
b'xe2x80x9065.400000'
>>> "-65.400000".encode('utf-8') # Replace with my minus
b'-65.400000'
xe2x80x90
is a hyphen character, see here: your hyphen is U+2010
and the hyphen-minus is U+002D
add a comment |
This is because your -
is not a minus sign but a hyphen character:
>>> "‐65.400000".encode('utf-8') # copy from your example
b'xe2x80x9065.400000'
>>> "-65.400000".encode('utf-8') # Replace with my minus
b'-65.400000'
xe2x80x90
is a hyphen character, see here: your hyphen is U+2010
and the hyphen-minus is U+002D
This is because your -
is not a minus sign but a hyphen character:
>>> "‐65.400000".encode('utf-8') # copy from your example
b'xe2x80x9065.400000'
>>> "-65.400000".encode('utf-8') # Replace with my minus
b'-65.400000'
xe2x80x90
is a hyphen character, see here: your hyphen is U+2010
and the hyphen-minus is U+002D
answered Nov 13 '18 at 0:26
Kevin Fang
1,266316
1,266316
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53271987%2ftrouble-with-negative-float-values-in-python-lists%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I jut copied your code into a python interpreter and it's not working either. I replaced the negative symbol with the one off my keyboard ( - ) and it worked. Are you using the correct negative symbol?
– Polymer
Nov 13 '18 at 0:23