generator

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

Why it seems contradictory when yield meet recursion in python?

Why it seems contradictory when yield meet recursion in python? Question: I tried to implement full permutation via python generator & recursion, but the output from ‘print’ is different from any other form of generator utility. def gen_perms(seq): seq_len = len(seq) def swap(i,j): x = seq[i] seq[i] = seq[j] seq[j] = x def perms(n): if …

Total answers: 1

Python: await the generator end

Python: await the generator end Question: Current versions of Python (Dec 2022) still allow using @coroutine decorator and a generation can be as: import asyncio asyncify = asyncio.coroutine data_ready = False # Status of a pipe, just to test def gen(): global data_ready while not data_ready: print("not ready") data_ready = True # Just to test …

Total answers: 1

yield from vs yield in for-loop

yield from vs yield in for-loop Question: My understanding of yield from is that it is similar to yielding every item from an iterable. Yet, I observe the different behavior in the following example. I have Class1 class Class1: def __init__(self, gen): self.gen = gen def __iter__(self): for el in self.gen: yield el and Class2 …

Total answers: 2

Generator with For In only returning first character instead of the whole string

Generator with For In only returning first character instead of the whole string Question: I want this generator to return these characters in a loop: "><><><" instead it’s only returning the first character ">>>>>>" if I replace yield with print it works correctly, why is it only returning the first character? def gn(): while True: …

Total answers: 1

how to connect three dataloaders together in pytorch – parallel not chained

how to connect three dataloaders together in pytorch – parallel not chained Question: i have three same length dataloaders (A,B,C) that load images (a,b,c), i want to create a new dataloader D that loads a dict of images, some syntax for clarity: usually the dataloader works like this: for a in A: a -> an …

Total answers: 1