Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.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 涉及列表的格式问题_Python_Function_Split_Ord - Fatal编程技术网

Python 涉及列表的格式问题

Python 涉及列表的格式问题,python,function,split,ord,Python,Function,Split,Ord,我有一个函数,它接受一个名称作为输入,将其放入一个列表,然后对其运行ord()。然而,我有一些(我相信的)格式问题。 我试图让它看起来像这样: b = (ascii value) a = (ascii value) t = (ascii value) m = (ascii value) a = (ascii value) n = (ascii value) 名称显示正确,但ascii值显示如下: b = [98, 97, 116, 109, 97, 110] a = [98, 97, 116,

我有一个函数,它接受一个名称作为输入,将其放入一个列表,然后对其运行ord()。然而,我有一些(我相信的)格式问题。 我试图让它看起来像这样:

b = (ascii value)
a = (ascii value)
t = (ascii value)
m = (ascii value)
a = (ascii value)
n = (ascii value)
名称显示正确,但ascii值显示如下:

b = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
t = [98, 97, 116, 109, 97, 110]
m = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
n = [98, 97, 116, 109, 97, 110]
def x():
     name = requestString("name")
     # usersName = list(name) # no need for this line, you can iterate over the string
     ascii = [orc(c) for c in name] #so this is just name
     for i, c in enumerate(name):  # use c for your character var name, 
          print c, "=", ascii[i]   # and enumerate provides the index
我不确定哪里出了问题,下面是我为它编写的代码:

def x():
     name = requestString("name")
     usersName = list(name)
     ascii = [orc(c) for c in usersName]
     for name in name: 
          print name, "=", ascii 
谢谢

编辑:
谢谢,非常感谢。现在就到我错的地方去

ascii
是所有字符的
ord
列表。要将它们与它们所代表的字符配对,请使用:


下面是对您的错误之处的一点回顾:

def x():
     name = requestString("name")
     usersName = list(name)
     ascii = [orc(c) for c in usersName] # here's the list
     for name in name: 
          print name, "=", ascii # and you're printing it here everytime
你可以像下面这样做:

b = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
t = [98, 97, 116, 109, 97, 110]
m = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
n = [98, 97, 116, 109, 97, 110]
def x():
     name = requestString("name")
     # usersName = list(name) # no need for this line, you can iterate over the string
     ascii = [orc(c) for c in name] #so this is just name
     for i, c in enumerate(name):  # use c for your character var name, 
          print c, "=", ascii[i]   # and enumerate provides the index
由于您没有返回任何内容,因此不需要创建列表,您还可以动态提供ord(c):

def print_ords_of_word(name):
    for c in name:
        print c, '=', ord(c)

您可以在单个循环中进行:

for item in name:
    print item, "=", ord(item)
演示:

[usersName中c的ord(c)]
usersName
中字母的所有
ord
值的列表。这不是你想在信的旁边写的——你只想写那封

您可以这样做:

def x():
    name = requestString("name")
    ascii = {letter:ord(letter) for letter in name}
    for letter in name:
        print(letter+" = "+ascii[letter])
        # or use string formatting which is better
或者只是:

def x():
    name = requestString("name")
    for letter in name:
        print(letter+" = "+ord(letter))

您将获得一个列表,因为您使用列表理解将整个usersName转换为一个ORD列表。这可以写在一行中,包括:

print "\n".join("{0} = {1}".format(c, ord(c)) for c in name)
字符串上的join方法采用序列或迭代器,并用字符串的原始内容分隔每个项(在本例中为\n)


调用join中的for循环创建了一个生成器,如果它有助于您更好地理解(但事实并非如此),您可以将其视为列表理解。

我认为这是最好的解决方案。伟大的头脑,嗯?:)@AaronHall同意,在纽约python会议上见,Aaron:)为了完整起见:
对于item,zip中的val(item,bytearray(item)):打印item,“=”,val
这不是一个实际的答案,但它使用了一个字节数组在迭代时给出整数的事实。。。我想你可能会觉得它很有趣:-)@JonClements当然,这绝对是一种开箱即用的东西。我敢打赌,你还有很多类似的技巧:)仅供参考,
为name in name
提供帮助不是一个好主意。将循环变量命名为您正在循环的集合以外的其他名称。(在本例中,经常使用
letter
char
ch
(缩写为
char
)等)