iterable

Python __iter__ and for loops

旧时模样 提交于 2019-12-22 09:26:44
问题 As I understand it, I can use the for loop construction on an object with a __iter__ method that returns an iterator. I have an object for which I implement the following __getattribute__ method: def __getattribute__(self,name): if name in ["read","readlines","readline","seek","__iter__","closed","fileno","flush","mode","tell","truncate","write","writelines","xreadlines"]: return getattr(self.file,name) return object.__getattribute__(self,name) I have an object of this class, a for which the

Scala Iterable Memory Leaks

假如想象 提交于 2019-12-22 05:46:49
问题 I recently started playing with Scala and ran across the following. Below are 4 different ways to iterate through the lines of a file, do some stuff, and write the result to another file. Some of these methods work as I would think (though using a lot of memory to do so) and some eat memory to no end. The idea was to wrap Scala's getLines Iterator as an Iterable. I don't care if it reads the file multiple times - that's what I expect it to do. Here's my repro code: class FileIterable(file:

How to implement “next” for a dictionary object to be iterable?

五迷三道 提交于 2019-12-22 00:44:12
问题 I've got the following wrapper for a dictionary: class MyDict: def __init__(self): self.container = {} def __setitem__(self, key, value): self.container[key] = value def __getitem__(self, key): return self.container[key] def __iter__(self): return self def next(self): pass dic = MyDict() dic['a'] = 1 dic['b'] = 2 for key in dic: print key My problem is that I don't know how to implement the next method to make MyDict iterable. Any advice would be appreciated. 回答1: Dictionaries are themselves

Why is there a method iterator() on java.util.Collection

纵然是瞬间 提交于 2019-12-21 07:08:19
问题 Why is there the method iterator() defined on the interface java.util.Collection when it already extends java.util.Iterable which has this very method defined. I'm thinking some sort of backward compatability or an opportunity to write some JavaDoc on the method at the collection level. Any other ideas? 回答1: Backwards compatibility. Iterable was not introducted until 1.5 with the for(Object o : iterable) construct. Previously, all collections had to provide a means to iterate them. 回答2: I

TypeError: 'type' object is not iterable - Iterating over object instances

旧街凉风 提交于 2019-12-21 05:39:23
问题 I am working on a project and I would like to make one of my classes iterable. To the best of my knowledge I can do that with using metaclass. First of all I would like to understand how metaclass works. Therefore I would like to present my own practicing example where I made a Car class. So here I would like to make my Car class objects iterable then I would like to print the names of them in the main function. The code example is the following: __author__ = 'mirind4' class IterableCar(type)

python counting elements in iterable with filter

浪尽此生 提交于 2019-12-20 06:23:26
问题 To count the elements in a list, you can use collections.Counter, but what if only some of the elements have to be counted? I've set up this example (please note: numpy is just for convenience. In general the list will contain arbitrary python objects): num_samples = 10000000 num_unique = 1000 numbers = np.random.randint(0, num_unique, num_samples) I would like to count how often a number occurs in this list, but I'm only interested in numbers <= 10. This is the baseline to beat. The Counter

Reversing a Range results in Mismatching Types

时光毁灭记忆、已成空白 提交于 2019-12-20 02:29:40
问题 I want to use a variable to hold what would normally be a range of something, for example Range<Int> , so that I can use conditional logic to change the range of a loop without copy/pasting the for loop. For instance: let range = aNumber % 2 == 0 ? 0..<10 : (0..<10).reverse() for i in range { /* for loop logic */ } The line let range = ... will result in the error: Result values in '? :' expression have mismatching types 'Range<Int>' and 'ReverseRandomAccessCollection<Range(Int)' . I would've

Finding lowest value within a nested list?

扶醉桌前 提交于 2019-12-19 10:46:13
问题 Im trying to write a function that takes a list and can print the lowest integer that is within that list. Now i'm trying to figure out what to do where this works with nested lists that if the lowest number is within one of those nested lists then overall it will print that number. My code is here: def listMin(): list2 = [3,4,[2,99,8],7] for i in range (len(list2)): if type(list2[i]) == type([]): y=min(i) list2.append(y) print "hello" if len(list2)== 0: return None else: x= min(list2) print

How do I yield a sequence of promises from an async iterator?

给你一囗甜甜゛ 提交于 2019-12-19 03:20:34
问题 I start with a function which renders and yields a sequence of image blobs. async function* renderAll(): AsyncIterableIterator<Blob> { const canvases = await getCanvases(); for (const canvas of canvases) { yield await new Promise<Blob>((resolve, reject) => { canvas.toBlob(result => { if (result) resolve(result); else reject(); }); }); } } That works fine, but the performance isn't ideal because each promise has to be resolved before starting on the next operation. Instead, the caller should

How to perform Stream functions on an Iterable? [duplicate]

元气小坏坏 提交于 2019-12-18 12:06:06
问题 This question already has answers here : Why does Iterable<T> not provide stream() and parallelStream() methods? (3 answers) Closed 5 years ago . In Java 8, the Stream class does not have any method to wrap a an Iterable . Instead, I am obtaining the Spliterator from the Iterable and then obtaining a Stream from StreamSupport like this: boolean parallel = true; StreamSupport.stream(spliterator(), parallel) .filter(Row::isEmpty) .collect(Collectors.toList()) .forEach(this::deleteRow); Is there