iterable

How to make a custom object iterable?

不问归期 提交于 2019-12-03 05:33:58
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) : def __init__(self, id) : self.id = id self.__stations = list() def __iter__(self): self.__i = 0 # iterable current item return iter(self.__stations) def __next__(self): if self.__i<len(self.__stations)-1: self.__i += 1 return self.__stations[self.__i] else: raise StopIteration I implemented __iter__

Scala: Exposing a JDBC ResultSet through a generator (iterable)

我是研究僧i 提交于 2019-12-03 05:26:33
I've got a set of rows in a database, and I'd like to provide an interface to spin through them like this: def findAll: Iterable[MyObject] Where we don't require having all the instances in memory at once. In C# you can easily create generators like this using yield, the compiler takes care of converting code that loops through the recordset into an iterator (sort of inverting it). My current code looks like this: def findAll: List[MyObject] = { val rs = getRs val values = new ListBuffer[MyObject] while ( rs.next() ) values += new valueFromResultSet(rs) values.toList } Is there a way I could

iter() not working with datetime.now()

痞子三分冷 提交于 2019-12-03 00:56:08
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 or has something changed in version 3.6.1? This is definitely a bug introduced in Python 3.6.0b1. The iter() implementation recently switched to using _PyObject_FastCall() (an optimisation, see issue 27128 ), and it must be this call that is breaking this. The same issue

How to get the count of Custom tuples against two lists

女生的网名这么多〃 提交于 2019-12-03 00:02:24
问题 Please help me to get the counter for the list SS2 in list SS1 in PYTHON using from collections import Counter or any other fastest way SS1 = [(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] SS2=[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)] Here is what i have tried

Expand an iterable element or non-iterable element into an array without checking element .length

依然范特西╮ 提交于 2019-12-02 21:57:54
问题 Given html <div></div> <div></div> calling document.querySelector("div") returns the first div element, where .length is not a property of the return value. Calling document.querySelectorAll() returns a NodeList having a .length property. The difference between the two return values of .querySelector() and .querySelectorAll() is that the former is not an iterable; and error will be thrown when attempting to use the spread element to expand the element into an array. In the following examples

Expand an iterable element or non-iterable element into an array without checking element .length

只愿长相守 提交于 2019-12-02 13:22:25
Given html <div></div> <div></div> calling document.querySelector("div") returns the first div element, where .length is not a property of the return value. Calling document.querySelectorAll() returns a NodeList having a .length property. The difference between the two return values of .querySelector() and .querySelectorAll() is that the former is not an iterable; and error will be thrown when attempting to use the spread element to expand the element into an array. In the following examples consider that either div or divs is a parameter received within the body of a functions call. Thus, as

How to get the count of Custom tuples against two lists

荒凉一梦 提交于 2019-12-02 13:04:36
Please help me to get the counter for the list SS2 in list SS1 in PYTHON using from collections import Counter or any other fastest way SS1 = [(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] SS2=[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)] Here is what i have tried and i don't know how to get the count for (1,2,4) th elements of the each tuple SS1=[(1, 2, 3, 4, 5), (1

Multiprocessing an iterable in python

泪湿孤枕 提交于 2019-12-02 11:45:09
问题 I am trying to split the following code to allow for multiprocessing in python and it is really becoming a frustrating task for me - I am new to multiprocessing and have read the documentation and as many samples as I could find but still have not found a solution that will have it work on all cpu cores at one time. I would like to split the iterables into quarters and have it compute the test in parrallel. My single thread example: import itertools as it import numpy as np wmod = np.array([

numpy.float64 is not iterable

放肆的年华 提交于 2019-12-02 08:28:01
I'm trying to print a function which uses several parameters from numpy array's and lists, but I keep getting the error "numpy.float 64 object is not iterable". I've looked at several questions on the forum about this topic and tried different answers but none seem to work (or I might be doing something wrong I'm still a beginner at python) but it all comes down to the same thing, I'm stuck and I hope you guys can help. I'm using python 2.7, this is the code: EDIT: Included the error message and changed the print to "print(T, (obj(T),))" from __future__ import division import numpy as np

Multiprocessing an iterable in python

一笑奈何 提交于 2019-12-02 04:52:29
I am trying to split the following code to allow for multiprocessing in python and it is really becoming a frustrating task for me - I am new to multiprocessing and have read the documentation and as many samples as I could find but still have not found a solution that will have it work on all cpu cores at one time. I would like to split the iterables into quarters and have it compute the test in parrallel. My single thread example: import itertools as it import numpy as np wmod = np.array([[0,1,2],[3,4,5],[6,7,3]]) pmod = np.array([[0,1,2],[3,4,5],[6,7,3]]) plines1 = it.product(wmod[0],wmod[1