About 433,000 results
Open links in new tab
  1. Understanding generators in Python - Stack Overflow

    May 20, 2018 · This allows the generator to be resumed at a later time, with execution continuing from the yield statement, and it can execute more code and return another value. Before Python 2.5 this was all generators did. Python 2.5 added the ability to pass values back in to the generator as well. In doing so, the passed-in value is available as an ...

  2. python - How to print the output of a generator - Stack Overflow

    Jan 14, 2016 · One should however be aware that iterating through the generator consumes it and if you don't have means of retrieving a new generator (for example if you got the generator as parameter to a function call) you would have to store the values - fx in an array:

  3. python - How to print a generator expression? - Stack Overflow

    list( generator-expression ) isn't printing the generator expression; it is generating a list (and then printing it in an interactive shell). Instead of generating a list, in Python 3, you could splat the generator expression into a print statement. Ie) print(*(generator-expression)). This prints the elements without commas and without brackets ...

  4. code generation - Python generating Python - Stack Overflow

    Oct 28, 2012 · This is pretty much the best way to generate Python source code. However, you can also generate Python executable code at runtime using the ast library. You can build code using the abstract syntax tree, then pass it to compile() to compile it into executable code. Then you can use eval() to run the code. I'm not sure whether there is a ...

  5. Python creating a generator that outputs multiple items

    Apr 22, 2019 · Is there a way in Python to generate multiple outputs at the same time. In particular I want something like: my_gen =(i for i in range(10)) and say I have a parameter batch_size = 3. I would want my generator to output: my_gen.next() 0,1,2 my_gen.next() 3,4,5 my_gen.next() 6,7,8 my_gen.next() 9,10

  6. Python: How do I save generator output into text file?

    I can print the generator output, but I can't figure out how to save that output into a text file. x = (1,2,2,4,1,3) avg = moving_average(x,2) for value in avg: print value When I change the print line to write to a file, output is printed to the screen, a file is created but it …

  7. python - How do I write a generator's output to a text file? - Stack ...

    May 28, 2021 · output = open(..., "w") and then either call output.close() or let Python do it for you (when output is collected by the memory manager). The "w" means that you are opening the file in write mode, as opposed to "r" (read, the default). There are various other options you can put here (+ for append, b for binary iirc).

  8. python - How to print the content of the generator ... - Stack …

    It's because you passed a generator to a function and that's what __repr__ method of this generator returns. If you want to print what it would generate, you can use: If you want to print what it would generate, you can use:

  9. python - How to loop through a generator - Stack Overflow

    Jul 18, 2012 · x = [tup[0] for tup in generator] If you just want to execute the generator without saving the results, you can skip variable assignment: # no var assignment b/c we don't need what print() returns [print(_) for _ in gen] Don't do this if your generator is infinite (say, streaming items from the internet).

  10. Python Simple Audio Tone Generator - Stack Overflow

    Mar 12, 2024 · 3- In python import numpy import pygame sampleRate = 44100 freq = 440 pygame.mixer.init(44100,-16,2,512) # sampling frequency, size, channels, buffer # Sampling frequency # Analog audio is recorded by sampling it 44,100 times per second, # and then these samples are used to reconstruct the audio signal # when playing it back.

Refresh