Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python generators

# Size of generators is a huge advantage compared to list
import sys

n= 80000

# List
a=[n**2 for n in range(n)]

# Generator
# Be aware of the syntax to create generators, lika a list comprehension but with round brakets
b=(n**2 for n in range(n))

print(f"List: {sys.getsizeof(a)} bits
Generator: {sys.getsizeof(b)} bits")
Comment

python generators

def generador():
    n = 1
    yield n

    n += 1
    yield n

    n += 1
    yield n
Comment

python generators with for

for i in generador():
    print(i)
# Salida: 1, 2, 3
Comment

PREVIOUS NEXT
Code Example
Python :: python return multiple value from a function 
Python :: add one element to tuple python 
Python :: drop variable pandas 
Python :: import from parent directory in python setup 
Python :: syntax of ternary operator 
Python :: python modules list 
Python :: python 3 string length 
Python :: what is scaling 
Python :: Show all column names and indexes dataframe python 
Python :: range(n,n) python 
Python :: add user agent selenium python canary 
Python :: bar break matplotlib 
Python :: python append many items to a list 
Python :: python mark function as no return 
Python :: list generation for python 
Python :: using pandas stack and subset to return a dataframe object of highly correated pairs 
Python :: python - dashboard 
Python :: Paraphrasing text with transformers library 
Python :: run persistent py script in background (good for flask) 
Python :: openCV error [WARN:0] terminating async callback 
Python :: #Combine two sets on python with for loop: reverse way in one line with space 
Python :: python with statement local variables 
Python :: python download from digital ocean spaces boto3 
Python :: limit entries in dataframe column 
Python :: print("ola") 
Python :: dependency parser tags 
Python :: hmac decrypt python 
Python :: cartpole dqn reward max is 200 
Python :: get number of occurrences of substring case independent python 
Python :: can i register a list in python for input 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =