How to use yield function in python

2024/10/12 16:28:18

SyntaxError: 'yield' outside function

>>> for x in range(10):
...     yield x*x
... File "<stdin>", line 2
SyntaxError: 'yield' outside function

what should I do? when I try to use simple yield in for loop.

Answer

EDIT

You referenced scala in you comment so I think that you are probably after a list comprehension:

>>> squares = [i*i for i in range(10)]
>>> squares
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

You can also use a generator expression:

>>> squares = (i*i for i in range(10))
>>> squares
<generator object <genexpr> at 0x7f5299e04690>
>>> list(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

You need to call yield from within a function. This makes the function a generator function. You can then iterate over successive values yielded by the function, for example:

def squares(N):for i in range(N):yield i*i>>> squares(10)
<generator object squares at 0x7f5299e04500>
>>> for n in squares(10):
...    print(n)
0
1
4
9
16
25
36
49
64
81>>> list(squares(100))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681, 1764, 1849, 1936, 2025, 2116, 2209, 2304, 2401, 2500, 2601, 2704, 2809, 2916, 3025, 3136, 3249, 3364, 3481, 3600, 3721, 3844, 3969, 4096, 4225, 4356, 4489, 4624, 4761, 4900, 5041, 5184, 5329, 5476, 5625, 5776, 5929, 6084, 6241, 6400, 6561, 6724, 6889, 7056, 7225, 7396, 7569, 7744, 7921, 8100, 8281, 8464, 8649, 8836, 9025, 9216, 9409, 9604, 9801]
https://en.xdnf.cn/q/69631.html

Related Q&A

Tkinter looks different on different computers

My tkinter window looks very different on different computers (running on the same resolution!):windows 8windows 7I want it to look like it does in the first one. Any ideas?My code looks like this:cla…

Sort when values are None or empty strings python

I have a list with dictionaries in which I sort them on different values. Im doing it with these lines of code:def orderBy(self, col, dir, objlist):if dir == asc:sorted_objects = sorted(objlist, key=la…

How to tell if you have multiple Djangos installed

In the process of trying to install django, I had a series of failures. I followed many different tutorials online and ended up trying to install it several times. I think I may have installed it twice…

Python Numerical Integration for Volume of Region

For a program, I need an algorithm to very quickly compute the volume of a solid. This shape is specified by a function that, given a point P(x,y,z), returns 1 if P is a point of the solid and 0 if P i…

invalid literal for int() with base 10: on Python-Django

i am learning django from official django tutorial. and i am getting this error when vote something from form. this caused from - probably - vote function under views.py here is my views.py / vote func…

How to use HTTP method DELETE on Google App Engine?

I can use this verb in the Python Windows SDK. But not in production. Why? What am I doing wrong?The error message includes (only seen via firebug or fiddler)Malformed requestor something like thatMy…

Python heapq : How do I sort the heap using nth element of the list of lists?

So I have lists of lists getting added to the heap; eg:n = [[1, 5, 93],[2, 6, 44],[4, 7, 45],[6, 3, 12]]heapq.heapify(n)print(n)This compares and sorts according to the lists first element.My question …

How strings are stored in python memory model

I am from c background and a beginner in python. I want to know how strings are actually stored in memory in case of python.I did something likes="foo"id(s)=140542718184424id(s[0])= 140542719…

The _imaging C module is not installed (on windows)

Im trying to generate some pdf with django/PIL/Imaging and everything is good until I attempt to put some images into the pdf:Exception Type: ImportError Exception Value: The _imaging C module is n…

HOW TO use fabric use with dtach,screen,is there some example

i have googled a lot,and in fabric faq also said use screen dtach with it ,but didnt find how to implement it? bellow is my wrong code,the sh will not execute as excepted it is a nohup taskdef dispatc…