简体   繁体   English

Python For循环和范围。 我的错是什么

[英]Python For loop and range. What is my mistake

I am trying to return the list that function will take in. This is what I have got: 我试图返回该函数将接受的列表。这就是我得到的:

def boo_list(my_list):
    for i in range(0, len(my_list) - 1, 1):
        print(i)

If I call for the function: 如果我要求该功能:

boo_list([True, True, False])

It returns: 它返回:

0 
1

Not

True
True
False

Where is the error in my logic? 我的逻辑错误在哪里?

Well, because you are dealing with the length and are printing the indices not the actual values. 好吧,因为您正在处理长度并且正在打印索引,而不是实际值。

Instead, do: 相反,请执行以下操作:

def boo_list(my_list):
    for i in my_list:
        print(i)

you can also use lambda function to print desire result. 您还可以使用lambda函数打印所需结果。

>>boo_list = lambda x:x

>>boo_list([True,False,True])

output: 输出:

#Result : [True, False, True]

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

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