> ") string_list = list(plain) print(string_li-6ren">
gpt4 book ai didi

python - 在 Python 中将字符串转换为整数

转载 作者:行者123 更新时间:2023-12-04 02:29:27 27 4
gpt4 key购买 nike

我试图在 python 中将字符串列表转换为整数,如下所示:

plain = input("string >> ")
string_list = list(plain)
print(string_list)
ASCII = []

for x in range (0, len(string_list)):
ASCII.append([ord(string_list[x])])

print(ASCII)

for x in range (0, len(ASCII)):
print(ASCII[x])
integer_ASCII = type(int(ASCII[x]))
print(integer_ASCII, end=", ")

但是我得到这个错误:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

那么有没有其他方法可以将字符串转换为整数。

最佳答案

不是 ascii 值,而是将列表附加到 ASCII首先,我们将每个字符串字符映射为 ASCII 值,并将其转换为字符串类型。然后使用 join 函数加入。

e = 100
n = 20
text = input("string >>>")
#This will return int ascii values for each character. Here you can do any arithmatic operations
rsa_values = list(map(lambda x:(ord(x)**e) % n , text))#[16, 1, 5, 16]


#TO get the same you can list compression also
rsa_values = [ (ord(x)**e) % n for x in text] #[16, 1, 5, 16]



#if you need to join them as string then use join function.
string_ascii_values = "".join(chr(i) for i in ascii_values)#'\x10\x01\x05\x10'

更新

基于科波菲尔的评论进行以下算术运算的更好方法

(ord(x)**e) % n

pow(ord(x), e, n)

rsa_values = [ pow(ord(x), e, n) for x in text] #[16, 1, 5, 16]

关于python - 在 Python 中将字符串转换为整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65261653/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com