简体   繁体   English

在列表的每个元素上使用 function

[英]Using a function on each element of a list

I have a list ( decrypted_list ) which contains four integer elements I have to decrypt.我有一个列表( decrypted_list ),其中包含我必须解密的四个 integer 元素。 The function's job is to decrypt the code, and it's logic is correct.该函数的工作是解密代码,它的逻辑是正确的。 After defining the function, I want to call it on every element of decrypted_list to decrypt its elements.在定义了 function 之后,我想在 decrypted_list 的每个元素上调用它来decrypted_list它的元素。

encrypted_list = [7, 1, 0, 2]

def decrypter(number):
    for number in encrypted_list:
        if (number + 4) < 10:
            return (number + 4)
        elif (number + 4) > 10:
            return (number + 4) % 10

decrypted_list = [decrypter(x) for x in encrypted_list]

However, the output is not what I expect:但是,output 不是我所期望的:

Expected: [1, 5, 4, 6]预期: [1、5、4、6]
Actual: [1, 1, 1, 1]实际: [1, 1, 1, 1]

Please help.请帮忙。 I am new to Python: :)我是 Python 的新手::)

There is no point in looping over encrypted_list in your decrypter function.decrypter器 function 中循环encrypted_list列表是没有意义的。 This function should just look at the number argument - it does not need to know about the encrypted_list variable.这个 function 应该只看number参数 - 它不需要知道encrypted_list变量。

The loop operation itself should only take place in your list comprehension ( [decrypter(x) for x in encrypted_list] ).循环操作本身应该只发生在您的列表理解中( [decrypter(x) for x in encrypted_list] )。

Change your function to the following and you should be fine:将您的 function 更改为以下内容,您应该没问题:

def decrypter(number):
  if (number + 4) < 10:
      return (number + 4)
  elif (number + 4) > 10:
      return (number + 4) % 10

Your function should look like this:您的 function 应如下所示:

def decrypter(number):
    if (number + 4) < 10:
        return (number + 4)
    elif (number + 4) > 10:
        return (number + 4) % 10

You shouldn't use a for loop here because you are going to use this function on each element of the list in the last line of your code:您不应该在此处使用 for 循环,因为您将在代码最后一行的列表的每个元素上使用此 function:

decrypted_list = [decrypter(x) for x in encrypted_list]

As you have said, that function only decrypts one number, so it doesn't need to know about the whole list.正如你所说,function 只解密一个数字,所以它不需要知道整个列表。 encrypted_list should not appear in that function. encrypted_list不应出现在该 function 中。

Adding that for loop in will cause the function's behaviour to change - whatever number you pass in, it will only decrypt the number 1 , because that is the first item of the list.添加该 for 循环将导致函数的行为发生变化 - 无论您传入什么数字,它只会解密数字1 ,因为这是列表的第一项。 The for loop doesn't actually loop through the list, because it hits return on the first iteration. for 循环实际上并不遍历列表,因为它在第一次迭代时就返回了。

Others already pointed out the problem in your code.其他人已经在您的代码中指出了问题。 I'd like to point out a redundancy in your function.我想指出您的 function 中的冗余。 This would produce the same output: decrypted_list = [(x + 4) % 10 for x in encrypted_list] .这将产生相同的 output:decrypted_list decrypted_list = [(x + 4) % 10 for x in encrypted_list] Or if you want to have in a function:或者,如果您想拥有 function:

def decrypter(number):
    return (number + 4) % 10

decrypted_list = [decrypter(x) for x in encrypted_list]

Since x % 10 == x if x < 10 .因为x % 10 == x如果x < 10

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

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