ValueError when converting string to int list
ValueError when converting string to int list
I´ve got a input:
"ABCDE"
and I want to convert the input to this:
np.array([A,B,C,D,E])
So that the output is an array with integers.
I´ve tried this:
s="ABCDE"
ss = list(s)
g = [int(i) for i in ss]
print(g)
but I get a
ValueError: invalid literal for int() with base 10: 'A'
np.array([A,B,C,D,E])
A
B
NameError
Just want the output to be an array or list. Like this: [A,B,C,D,E]
– CEFOG
Aug 21 at 8:18
@CEFOG, so you want array of integers or array of chars (strings)?
– taras
Aug 21 at 8:22
chars, I guess(because later I have to try to convert the letters to values. like A=1, B=5,C=10,D=50 and E=100)
– CEFOG
Aug 21 at 8:37
You have to convert them later; do you use the char values before that, or do you want an immediate conversion? How is the conversion defined?
– 9769953
Aug 21 at 11:12
2 Answers
2
Assuming that A is to be represented as 1, B as 2 and so on.
A
1
B
2
s="ABCDE"
ss = list(s)
g = [ord(i) - ord('A') +1 for i in ss]
print(g)
here ord(i) converts the letter to it's corresponding value in the character table and we subtract the value for A.
ord(i)
A
you should use this syntax g = [int(i,16) for i in ss] to indicate that your are using the hex format.
g = [int(i,16) for i in ss]
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
How is
np.array([A,B,C,D,E])an array of integers? UnlessA,Betc are defined variables, that results in aNameError.– 9769953
Aug 21 at 8:12