简体   繁体   English

打印列表仅输出一个结果

[英]Printing list only outputs one result

I have this code that I used for a python challenge, now it use to work perfectly, but now the only result I get as an output is 'i'. 我有用于python挑战的这段代码,现在它可以完美地工作,但是现在我作为输出获得的唯一结果是“ i”。

I'm not sure but I'm assuming that my code isn't storing all the values I'm giving ascii_list for some reason. 我不确定,但是我假设我的代码由于某种原因没有存储我提供给ascii_list的所有值。

a = """g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj.""" 
def ascii_list(a):
    for i in a: return chr(ord(i)+2)

alist = ascii_list(a)

print ("".join(alist))

Now my question is, why is it doing this and where have I gone wrong? 现在我的问题是,为什么要这样做?我在哪里出错了? I'm using Python 3.3. 我正在使用Python 3.3。

(Go easy on me I've tried to do this right) (请放心,我尝试过正确的操作)

The problem is here: 问题在这里:

def ascii_list(a):
    for i in a: return chr(ord(i)+2)

The first time through the loop, you return . 第一次通过循环,您将return That ends the function, and the loop, on the first value. 这样就结束了函数和第一个值的循环。

You could change this into a generator function: 您可以将其更改为生成器函数:

def ascii_list(a):
    for i in a: yield chr(ord(i)+2)

Intuitively, this is pretty simple. 直观地讲,这很简单。 You can yield as many times as you want, instead of return ing one value. 您可以根据需要yield多次,而不必return一个值。 When someone tries to loop over the result of your function, or pass it to a function like list or join , it will get all the yield ed results. 当有人尝试遍历函数的结果,或将其传递给listjoin类的函数时,它将获得所有yield结果。

But if this is too confusing, and you're not ready to learn about generators yet, you could also build up a list explicitly: 但是,如果这太令人困惑,并且您还没有准备好了解生成器,那么还可以显式地建立一个列表:

def ascii_list(a):
    return [chr(ord(i)+2) for i in a]

… or: … 要么:

def ascii_list(a):
    result = []
    for i in a: result.append(chr(ord(i)+2))
    return result

As Jon Clements pointed out in a comment, this isn't quite the right algorithm for what you're trying to do. 正如乔恩·克莱门茨(Jon Clements)在评论中指出的那样,这并不是您要尝试执行的正确算法。 First, you need to "roll over" past the end of the alphabet—otherwise, you get { instead of a and | 首先,您需要“翻转”字母的末尾,否则,您将得到{而不是a| instead of b . 代替b If you understand the % (modulus/remainder) operator, you can do this in a single step; 如果您了解% (模数/余数)运算符,则可以一步完成。 if not, you can use an if / else . 如果不是,则可以使用if / else Second, you don't want to translate non-letters like space or . 其次,您不希望翻译非字母,例如space或. at all; 根本 for this, you probably want and if / else . 为此,您可能需要if / else Writing this out verbosely: 详细地写出来:

def ascii_list(a):
    for i in a:
        if not i.isalpha():
            rot = 0
        else if i.lower() > 'x':
            rot = 2 - 26
        else:
            rot = 2
        yield chr(ord(i) + rot)

That's because you're returning in the first pass of the loop. 那是因为您要在循环的第一遍中返回。 You really want a list comprehension: 您确实想要列表理解:

return [chr(ord(i)+2) for i in a]

You start iterating the string, get to character 'g', then return chr(ord('g') + 2). 您开始迭代字符串,到达字符“ g”,然后返回chr(ord('g')+ 2)。 That returns 'i' and terminates the loop. 返回“ i”并终止循环。

In addition to a list comprehension, you can also use a map with your method: 除了列表理解之外,您还可以将map与您的方法一起使用:

def get_code(i):
    return chr(ord(i)+2)

''.join(map(get_code, a))

Or a more compact version, with a lambda: 或更紧凑的版本,带有lambda:

''.join(map(lambda i: chr(ord(i)+2), a))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM