Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将字符列表更改为ASCII格式?_Python_List_Integer_Character_Decimal - Fatal编程技术网

Python 如何将字符列表更改为ASCII格式?

Python 如何将字符列表更改为ASCII格式?,python,list,integer,character,decimal,Python,List,Integer,Character,Decimal,我想知道如何将字符列表转换为ascii格式(a=97、x=120等)。我曾尝试使用循环将每个单独的项转换为十进制形式,但我想知道是否有更好的方法。 到目前为止,我已经写了这篇文章,但这似乎不起作用 x = input().lower() message = list( x ) messageInt = [] i = 0 while True: messageInt.append( ord(message([i])) i += 1 print messageInt

我想知道如何将字符列表转换为ascii格式(a=97、x=120等)。我曾尝试使用循环将每个单独的项转换为十进制形式,但我想知道是否有更好的方法。 到目前为止,我已经写了这篇文章,但这似乎不起作用

x = input().lower()
message = list( x ) 
messageInt = []
i = 0
while True:
        messageInt.append( ord(message([i]))
        i += 1
print messageInt
我如何让我的代码产生这样的结果
[101、120、101、97、109、112、108、101]
如果输入是单词“example”。

这里是:

s = "string here"
a = list(map(ord,list(s.lower())))
a
包含数字列表

要制作类似于您的问题的内容,请执行以下操作:

print(list(map(ord,list(input().lower()))))
给你:

s = "string here"
a = list(map(ord,list(s.lower())))
a
包含数字列表

要制作类似于您的问题的内容,请执行以下操作:

print(list(map(ord,list(input().lower()))))

由于要将字符串转换为其等效整数,因此需要使用
int()
函数将字符串转换为等效整数,但要转换列表中的所有元素,因此,您可以使用map函数,它接受两个参数函数和迭代器,然后它将函数应用于迭代器的所有元素

如果您使用的是python2,则返回一个列表;如果您使用的是Python3,则它将返回一个映射对象,因此您可以通过应用
list()


由于要将字符串转换为其等效整数,因此需要使用
int()
函数将字符串转换为等效整数,但要转换列表中的所有元素,因此,您可以使用map函数,它接受两个参数函数和迭代器,然后它将函数应用于迭代器的所有元素

如果您使用的是python2,则返回一个列表;如果您使用的是Python3,则它将返回一个映射对象,因此您可以通过应用
list()


a_list=[ord(i)for i in input().lower()]
这是一个无限循环,你意识到了吗?
a_list=[ord(i)for i in input().lower()]
这是一个无限循环,你意识到了吗?非常感谢!非常感谢你!