简体   繁体   English

从 function 返回列表的元素

[英]To return elements of a list from a function

I want to define a function that takes a list as its argument and then returns the elements in order.我想定义一个 function ,它将一个列表作为其参数,然后按顺序返回元素。

For example:例如:

def iterator(lis):
    for e in range(len(lis)):
        return lis[e]

l=input("Enter list elements:").split()
x=iterator(l)
print(x)

But this just returns the first value of the list as:但这只是将列表的第一个值返回为:

Enter list elements:12 23 34
12

How can I print all the values in successive lines?如何打印连续行中的所有值?

You can use yield in order to build a generator, here's the official documentation about Generators你可以使用 yield 来构建一个生成器,这里是关于生成器的官方文档

What is a generator in Python? Python 中的生成器是什么?

A Python generator is a function which returns a generator iterator (just an object we can iterate over) by calling yield.一个 Python 生成器是一个 function 通过调用 yield 返回一个生成器迭代器(只是一个 object 我们可以迭代)。 yield may be called with a value, in which case that value is treated as the "generated" value. yield 可以用一个值调用,在这种情况下,该值被视为“生成的”值。

I also want to share an example, be sure to read the comments:我也想分享一个例子,请务必阅读评论:

def iterator(lis):
    for e in range(len(lis)):
        yield lis[e]

l=input("Enter list elements:").split()

# A generator returns an Iterable so you should
# loop to print
for number in iterator(l):
    print(number)

# Or use list
result = list(iterator(l))
print(result)

Output Output
1 1
2 2
3 3
['1', '2', '3'] ['1', '2', '3']

You probably want yield , as return causes the function call to end immediately.您可能想要yield ,因为return会导致 function 调用立即结束。 But yield just produces another iterator;但是yield只是产生另一个迭代器; you still need to iterate over the result to print them all, rather than simply printing the iterator itself.您仍然需要遍历结果以将它们全部打印出来,而不是简单地打印迭代器本身。

def iterator(lis):
    for e in range(len(lis)):
        yield lis[e]

...
for element in x:
    print(element)

Of course, you are pretty much just reimplementing the existing list iterator here;当然,您几乎只是在此处重新实现现有的列表迭代器; an equivalent definition would be一个等效的定义是

def iterator(lis):
    yield from lis

What you might want instead is to do something like你可能想要的是做类似的事情

x = '\n'.join(l)
print(x)

which creates a string by iterating over l and joining the elements using \n .它通过迭代l并使用\n连接元素来创建一个字符串。 The resulting multiline string can then be printed.然后可以打印生成的多行字符串。

It will print only one element if you do return如果您确实return ,它将只打印一个元素

def iterator(lis):
    for e in range(len(lis)):
        return lis[e]
l=input("Enter list elements:").split()
x=iterator(l)
for y in x: print(y)

Use: def iterator(list): for e in range(len(list)): a= list[e] print(a) l=a,b,c=input().split() a=iterator(l)使用: def iterator(list): for e in range(len(list)): a= list[e] print(a) l=a,b,c=input().split() a=iterator(l)

Use:利用:

[print(i) for i in input("Enter list elements:").split(" ")]

return causes the function to stop after it hits the statement. return导致 function 在到达语句后停止。 So your for loop only ever runs once.所以你的 for 循环只运行一次。

You could use yield as mentioned in the other answers, but I really don't think you need a function in this situation.您可以使用其他答案中提到的 yield ,但我真的认为在这种情况下您不需要 function 。 Because the function is just going to return the list that it took as an argument.因为 function 只是要返回它作为参数的列表。 What you should do is something like this:你应该做的是这样的:

i = input("Enter list elements: ").split()
for x in i:
   print(x)

It's that simple.就是这么简单。

def iterator(lis):
    for e in range(len(lis)):
        print( lis[e])

l=input("Enter list elements:").split()
iterator(l)

if you want to do some operations for each item in the list, then you should accomodate those within the function.如果您想对列表中的每个项目进行一些操作,那么您应该在 function 中容纳这些操作。

Use of print instead of return will give you the expected output..try it once使用 print 而不是 return 会给你预期的 output .. 试一次

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

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