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'





How is np.array([A,B,C,D,E]) an array of integers? Unless A, B etc are defined variables, that results in a NameError.
– 9769953
Aug 21 at 8:12


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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

Node.js puppeteer - Use values from array in a loop to cycle through pages