从函数列表中返回项目。蟒蛇 [英] Return items from list in function. Python

查看:106
本文介绍了从函数列表中返回项目。蟒蛇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  my_list = ['a','b','c'] 

def func(输入):

print i
print func(my_list)

输出

  a 
b
c

我不希望'None'如此我如何做一行代码来返回输入列表中的项目?



我试过:

 我在输入中输入:print i 

但它不起作用,因为return语句不能像那样工作


<您正在打印来自 func 的返回码,它是 none 。只需调用 func(my_list),而不需要 print


my_list = ['a','b','c']

def func(input):
    for i in input:
        print i
print func(my_list)

Output

a
b
c
None

I don't want the 'None' so how I can do a single line of code to return the items in my input list?

I tried:

return for i in input: print i

but it didn't work because return statements don't work like that

解决方案

You are printing the return code from func which is none. Just call func(my_list) without the print.

这篇关于从函数列表中返回项目。蟒蛇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆