Skip to content

Magenaut

  • Home
  • Topics
    • Notes
    • Tutorial
    • Bug fixing
    • Extension
    • Server
  • Q&A
  • Privacy Policy
  • About

generator

What’s the most Pythonic way to identify consecutive duplicates in a list?

August 19, 2022 by Magenaut

I’ve got a list of integers and I want to be able to identify contiguous blocks of duplicates: that is, I want to produce an order-preserving list of duples where each duples contains (int_in_question, number of occurrences).

Categories Python, Q&A Tags duplicates, generator, list, python Leave a comment

How do I build a numpy array from a generator?

August 19, 2022 by Magenaut

How can I build a numpy array out of a generator object?

Categories Python, Q&A Tags generator, numpy, python Leave a comment

Split a generator into chunks without pre-walking it

August 18, 2022 by Magenaut

(This question is related to this one and this one, but those are pre-walking the generator, which is exactly what I want to avoid)

Categories Python, Q&A Tags generator, python Leave a comment

Python random sample with a generator / iterable / iterator

August 17, 2022 by Magenaut

Do you know if there is a way to get python’s random.sample to work with a generator object. I am trying to get a random sample from a very large text corpus. The problem is that random.sample() raises the following error.

Categories Python, Q&A Tags generator, python, random Leave a comment

What’s the shortest way to count the number of items in a generator/iterator?

August 17, 2022 by Magenaut

If I want the number of items in an iterable without caring about the elements themselves, what would be the pythonic way to get that? Right now, I would define

Categories Python, Q&A Tags generator, iterable, iterator, python Leave a comment

Can iterators be reset in Python?

August 17, 2022 by Magenaut

Can I reset an iterator / generator in Python? I am using DictReader and would like to reset it to the beginning of the file.

Categories Python, Q&A Tags generator, iterator, python Leave a comment

how to split an iterable in constant-size chunks

August 17, 2022 by Magenaut

Possible Duplicate:
How do you split a list into evenly sized chunks in Python?

Categories Python, Q&A Tags algorithm, chunking, generator, python Leave a comment

If range() is a generator in Python 3.3, why can I not call next() on a range?

August 17, 2022 by Magenaut

Perhaps I’ve fallen victim to misinformation on the web, but I think it’s more likely just that I’ve misunderstood something. Based on what I’ve learned so far, range() is a generator, and generators can be used as iterators. However, this code:

Categories Python, Q&A Tags generator, iterator, python, python-3.x, typeerror Leave a comment

How to look ahead one element (peek) in a Python generator?

August 16, 2022 by Magenaut

I can’t figure out how to look ahead one element in a Python generator. As soon as I look it’s gone.

Categories Python, Q&A Tags generator, peek, python Leave a comment

yield in list comprehensions and generator expressions

August 16, 2022 by Magenaut

The following behaviour seems rather counterintuitive to me (Python 3.4):

Categories Python, Q&A Tags generator, generator-expression, list-comprehension, python, yield Leave a comment
Older posts
Newer posts
← Previous Page1 Page2 Page3 Page4 Next →
  1. michealSmith07 on Is there a way to dynamically refresh the less command?August 21, 2022

    That is a very nice post. I like this post.

  2. anonymous on Fix libwacom9 dependency issue when upgrade DebianJune 27, 2022

    saved my day!! Thanks for the help…

  3. sreedhar on Fix libwacom9 dependency issue when upgrade DebianMay 10, 2022

    Thanks its working

  4. saintnick on Fix libwacom9 dependency issue when upgrade DebianMay 10, 2022

    remove libwacom2 worked for me as well

  5. ranafoul on Fix libwacom9 dependency issue when upgrade DebianApril 22, 2022

    apt remove libwacom2 helped on kali 2022.01. gr8

.net ajax asp.net asp.net-core asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 asp.net-web-api bash c# command-line css custom-post-types custom-taxonomy dataframe dictionary django entity-framework functions gridview html iis javascript jquery json linux list matplotlib numpy pandas php plugin-development plugins posts python python-2.7 python-3.x security shell shell-script sql string vb.net webforms wp-query

© 2026 Magenaut • Built with GeneratePress