iterator

How do I implement custom iterators so that I can nest them?

How do I implement custom iterators so that I can nest them? Question: I was just looking up some stuff about python iterators and stumbled across thisW3School iterator example example from w3school: class MyNumbers: def __iter__(self): self.a = 1 return self def __next__(self): if self.a <= 20: x = self.a self.a += 1 return x …

Total answers: 1

Is the function "next" a good practice to find first occurrence in a iterable?

Is the function "next" a good practice to find first occurrence in a iterable? Question: I’ve learned about iterators and such and discovered this quite interesting way of getting the first element in a list that a condition is applied (and also with default value in case we don’t find it): first_occurence = next((x for …

Total answers: 2

How to reset and shuffle a "next" iterator?

How to reset and shuffle a "next" iterator? Question: I have a function that generates numbers from -180 to 180: all_angles = list(range(-180,180)) random.shuffle(all_angles) next_angle = iter(all_angles) The issue is that it stops generating after 360 (which makes sense since it’s from -180 to 180): n_list = [] for i in range(1000): n_list.append(next(next_angle)) print(len(n_list)) >>> …

Total answers: 3

How do I save the output of a Python generator into memory?

How do I save the output of a Python generator into memory? Question: This question probably has a straightforward answer, but I’m not finding it. I’m working with the Calendar class in Lib/calendar.py and there are several methods associated with that class that generate everything from date objects to tuples with year, month, day as …

Total answers: 2

Defining `__iter__` method for a dask actor?

Defining `__iter__` method for a dask actor? Question: Is it possible for a dask actor to have an __iter__ method as defined by a class? Consider this example adapted from the docs: class Counter: """A simple class to manage an incrementing counter""" def __init__(self): self.n = 0 def increment(self): self.n += 1 return self.n def …

Total answers: 1

Idiomatic Rust index filtering (numpy style)

Idiomatic Rust index filtering (numpy style) Question: A common idiom that I use in python for initializing arrays is arr = np.zeros(10) x = np.linspace(-5,5,10) arr[np.abs(x)<2] = 1. That is, conditionally changing elements of an array using a "view". Is there an idiomatic way of doing this (also with a vector or other collection) in …

Total answers: 2

Why does a list of generators only return the elements of the last generator?

Why does a list of generators only return the elements of the last generator? Question: I’m given an arbitrary list of objects (e.g. [‘foo’, ‘bar’]). My goal is to produce a list of equal size where every element in the result list is a generator that repeats the respective input element 5 times. This is …

Total answers: 1

How to stop sage graph generating iterator after N iterations?

How to stop sage graph generating iterator after N iterations? Question: I want to create N planar graphs using sagemath’s graph.planar_graphs iterator. The problem is that this function return all possible planar graphs given a certain number of vertices. What I want to do is something like: while len(gen) < N: gen = list(graphs.planar_graphs(50)) So …

Total answers: 1

How to repeat list's elements with each different n times and in Python?

How to repeat list's elements with each different n times and in Python? Question: The idea is to repeat list’s elements with each different n times as below. ls = [7, 3, 11, 5, 2, 3, 4, 4, 2, 3] id_list_fname = [‘S11’, ‘S15’, ‘S16’, ‘S17’, ‘S19’, ‘S3’, ‘S4’, ‘S5’, ‘S6’, ‘S9’] all_ls = [] …

Total answers: 3