iterator

Refactoring pandas using an iterator via chunksize

Refactoring pandas using an iterator via chunksize Question: I am looking for advice on using a pandas iterator. I performed a parsing operation using Python pandas, the size of the input files (a bioinformatics program called eggNOG) is resulting in ‘RAM bottleneck’ phenomenon. It’s just not processing the file. The obvious solution is to shift …

Total answers: 2

Python generator vs iterator

Python generator vs iterator Question: So I have custom iterator which represents as a class CustomIterator below. The last print shows a size of all data which it uses when gets one char from some string. 1170 bytes. class CustomIterator: def __init__(self, collection: str): self.__position = 0 self.__collection = collection def __iter__(self): return self def …

Total answers: 1

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