iterator

Lazily transpose dimensions 0 and 2 in iterator model

Lazily transpose dimensions 0 and 2 in iterator model Question: Given an iterable of an iterable of an iterable it_it_it (i.e. a lazy representation of 3d array) you can lazily transpose dimensions 0 and 1 by zip(*it_it_it) and dimensions 1 and 2 by map(lambda it_it: zip(*it_it), it_it_it). However, the last combination (0 and 2) is …

Total answers: 3

Gather items that cause all() to return false

Gather items that cause all() to return false Question: This question is about what one can/cannot do with all() function. I would like to identify all elements which fail the condition set in all() function. In the example below all() will return False since not all elements are of type int. ‘c’ and ‘5’ will …

Total answers: 2

How to create lists from pandas columns

How to create lists from pandas columns Question: I have created a pandas dataframe using this code: import numpy as np import pandas as pd ds = {‘col1’: [1,2,3,3,3,6,7,8,9,10]} df = pd.DataFrame(data=ds) The dataframe looks like this: print(df) col1 0 1 1 2 2 3 3 3 4 3 5 6 6 7 7 8 …

Total answers: 4

RuntimeError raised for StopIteration on a for loop

RuntimeError raised for StopIteration on a for loop Question: I want to create an Iterator that runs a couple of checks and yield an object that follows all the checks, if no object is found, it raises a StopIteration to exit loops. Something similar to the code below: def gen(): for i in range(3): yield …

Total answers: 1

Using the same iterator in nested for loops

Using the same iterator in nested for loops Question: Consider the following code: from itertools import chain lst = [‘a’, 1, 2, 3, ‘b’, 4, 5, ‘c’, 6] def nestedForLoops(): it = iter(lst) for item0 in it: if isinstance(item0, str): print(item0) else: # this shouldn’t happen because of # 1. lst[0] is a str, and …

Total answers: 2

How to read multiple images from two different folders one by one using python?

How to read multiple images from two different folders one by one using python? Question: I am new to python and in a learning stage. I am trying to read multiple images from two different folders in a similar order. Each folder contains 147 images. In one folder there is thermal image (FLIR1271) and second …

Total answers: 1

Find palindrome python space complexity?

Find palindrome python space complexity? Question: Given the following code in python which checks whether an string of n size is palindromic: def is_palindromic(s): return all(s[i] == s[~i] for i in range(len(s) // 2)) What is the space complexity of this code? Is it O(1) or O(n)? The all function gets an iterable as a …

Total answers: 2

Python print to csv in iteration, save rows without line spaces

Python print to csv in iteration, save rows without line spaces Question: Hello I will save this output to a csv file after iteration, print is ok, but csv not. Below are the print output 1580933840499281920 2022-10-14 14:49:34+00:00 illiabogdanov en RT @alexstubb: Dear @elonmusk, the war in Ukraine is about life and death, freedom and …

Total answers: 2

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