iterable

tqdm: extract time passed + time remaining?

tqdm: extract time passed + time remaining? Question: I have been going over the tqdm docs, but no matter where I look, I cannot find a method by which to extract the time passed and estimated time remaining fields (basically the center of the progress bar on each line: 00:00<00:02). 0%| | 0/200 [00:00<?, ?it/s] …

Total answers: 4

collections.Iterable vs typing.Iterable in type annotation and checking for Iterable

collections.Iterable vs typing.Iterable in type annotation and checking for Iterable Question: I found that in Python both collections.Iterable and typing.Iterable can be used in type annotation and checking for whether an object is iterable, i.e., both isinstance(obj, collections.Iterable) and isinstance(obj, typing.Iterable) works. My question is, what are the differences among them? And which one is …

Total answers: 1

How come regex match objects aren't iterable even though they implement __getitem__?

How come regex match objects aren't iterable even though they implement __getitem__? Question: As you may know, implementing a __getitem__ method makes a class iterable: class IterableDemo: def __getitem__(self, index): if index > 3: raise IndexError return index demo = IterableDemo() print(demo[2]) # 2 print(list(demo)) # [0, 1, 2, 3] print(hasattr(demo, ‘__iter__’)) # False However, …

Total answers: 1

iter() not working with datetime.now()

iter() not working with datetime.now() Question: A simple snippet in Python 3.6.1: import datetime j = iter(datetime.datetime.now, None) next(j) returns: Traceback (most recent call last): File “<stdin>”, line 1, in <module> StopIteration instead of printing out the classic now() behavior with each next(). I’ve seen similar code working in Python 3.3, am I missing something …

Total answers: 2

Finding the Max value in a two dimensional Array

Finding the Max value in a two dimensional Array Question: I’m trying to find an elegant way to find the max value in a two-dimensional array. for example for this array: [0, 0, 1, 0, 0, 1] [0, 1, 0, 2, 0, 0][0, 0, 2, 0, 0, 1][0, 1, 0, 3, 0, 0][0, 0, 0, …

Total answers: 7

TypeError: NoneType object not iterable

TypeError: NoneType object not iterable Question: It might be silly question but I just started to learn OOP in Python and I am struggling to get this one: The following code works fine: class Name(object): def __init__(self): self.name=None C1=’Stephen Curry’ C2=’Kevin Durant’ C3=’LeBron James’ C4=’Chris Paul’ nameList=[C1,C2,C3,C4] nameList.sort() for e in nameList: print (e) However, …

Total answers: 1

Opposite of any() function

Opposite of any() function Question: The Python built-in function any(iterable) can help to quickly check if any bool(element) is True in a iterable type. >>> l = [None, False, 0] >>> any(l) False >>> l = [None, 1, 0] >>> any(l) True But is there an elegant way or function in Python that could achieve …

Total answers: 4

how to convert pandas series to tuple of index and value

how to convert pandas series to tuple of index and value Question: I’m looking for an efficient way to convert a series to a tuple of its index with its values. s = pd.Series([1, 2, 3], [‘a’, ‘b’, ‘c’]) I want an array, list, series, some iterable: [(1, ‘a’), (2, ‘b’), (3, ‘c’)] Asked By: …

Total answers: 3

What is the most pythonic way to use len on a scalar?

What is the most pythonic way to use len on a scalar? Question: I read this question python: how to identify if a variable is an array or a scalar but when using the following code I get a false on an np.array as can be demonstrated below. import collections isinstance(np.arange(10), collections.Sequence) # returns false …

Total answers: 4

How to make a custom object iterable?

How to make a custom object iterable? Question: I have a list of custom-class objects (sample is below). Using: list(itertools.chain.from_iterable(myBigList)) I wanted to “merge” all of the stations sublists into one big list. So I thought I need to make my custom class an iterable. Here is a sample of my custom class. class direction(object) …

Total answers: 3