generator

Sequential chaining of itertools operators

Sequential chaining of itertools operators Question: I’m looking for a nice way to sequentially combine two itertools operators. As an example, suppose we want to select numbers from a generator sequence less than a threshold, after having gotten past that threshold. For a threshold of 12000, these would correspond to it.takewhile(lambda x: x<12000) and it.takewhile(lambda …

Total answers: 1

Python generator vs iterator

Python generator vs iterator Question: So I have custom iterator which represents as a class CustomIterator below. The last print shows a size of all data which it uses when gets one char from some string. 1170 bytes. class CustomIterator: def __init__(self, collection: str): self.__position = 0 self.__collection = collection def __iter__(self): return self def …

Total answers: 1

How does generator * / splat unpacking work?

How does generator * / splat unpacking work? Question: Let’s say I have some function f that will accept a variable number of arguments: def f(*args): for a in args: print(a) Below I call on this function three ways: Case 1: Passing in a generator expression without parenthesis: f(l for l in range(5)) >>> <generator …

Total answers: 1

Weird behaviour of a generator function while converting it to a list

Weird behaviour of a generator function while converting it to a list Question: I am trying to get every state of a list while it is being sorted for a visualization. So with bubbleSort algorithm i made a generator function : def bubbleSort(arr): n = len(arr) yield arr # yielding original state for i in …

Total answers: 1

Variable in generator function shadows name from outer scope

Variable in generator function shadows name from outer scope Question: I recently started to teach myself Python and currently work on generator functions. Here, I encountered a "scoping" issue with variable names inside of the generator shadowing names in outer scope. I did some research on this but could not come up with an explanation. …

Total answers: 1

Return several values inside for loop

Return several values inside for loop Question: I have a function that must return several values using a for loop. I do not wish to store the values inside a list or a dict. Because of the use of the return, I only get the first value. How can I return all values successively? I …

Total answers: 2

Random dont chose 8 character words

Random dont chose 8 character words Question: I used random module in my ptoject. Im not a programmer, but i need to do a script which will create a randomized database from another database. In my case, I need the script to select random words lined up in a column from the "wordlist.txt" file, and …

Total answers: 1