Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Generators

>>> mygenerator = (x*x for x in range(3))
>>> for i in mygenerator:
...    print(i)
0
1
4
Comment

# generators

# generators
gen_1= (x*x for x in [1,2,3])   # [1, 4, 9]
gen_2 = (x+x for x in gen_1)  # [2, 8, 18]
for i in gen_2:
   print(i)

# 2
# 8
# 18
 
for i in gen_1:
   print(i)


# Nothing is displayed
# With generators, instead of storing data in the variable gen_1 (and therefore in memory), you are going to generate them on the spot (e.g. only when you need them).
# Careful: Generating data on the spot does not allow to read them several times. And if you try to do so, no error will be raised to warn you.
Comment

PREVIOUS NEXT
Code Example
Python :: plotly dcc.interval bar graph with time 
Python :: fill variable based on values of other variables python 
Python :: math.floor python 
Python :: # sort the dictionary 
Python :: matplotlib get colorwheel 
Python :: pairplot legend position 
Python :: python math.trunc 
Python :: how delet an obj from memori in python 
Python :: xgb model prediction changes if i save and load the model 
Python :: Specifying your data type 
Python :: search a number in 2d sorted 
Python :: aws chalice 
Python :: Convert Int to String Using F-strings 
Python :: Math Module asin() Function in python 
Python :: pyqt global hotkey 
Python :: Example of importing module in python 
Python :: python set vs tuple performance 
Python :: which can be reversed , string or list? 
Python :: install python 3.10 pip 
Python :: apply WEKA filter on customer dataset 
Python :: Python NumPy Shape function example verifying the value of last dimension 
Python :: how to extract a list of values from numpy array using index list 
Python :: Python NumPy block Function Syntax 
Python :: Python NumPy insert Function Example Working with Scalars 
Python :: python __div__ 
Python :: python mxs Classof 
Python :: Snippet for inverse a matrix using numpy 
Python :: using .get() for deep dictionary 
Python :: Python pattern of 1010101 
Python :: knn compute_distances_one_loop 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =