iterable

Using Objects in For Of Loops

时光毁灭记忆、已成空白 提交于 2019-12-27 17:06:34
问题 Why isn't is possible to use objects in for of loops? Or is this a browser bug? This code doesn't work in Chrome 42, saying undefined is not a function: test = { first: "one"} for(var item of test) { console.log(item) } 回答1: The for..of loop only supports iterable objects like arrays, not objects. To iterate over the values of an object, use: for (var key in test) { var item = test[key]; } 回答2: You can use this syntax: let myObject = {first: "one"}; for(let [key, value] of Object.entries

Can I store an iterator in a file which I can read from later? Will this reduce space consumption? [closed]

谁说我不能喝 提交于 2019-12-25 19:46:18
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 7 years ago . Let's say I have a very large integer, around the order of 10**200. Now storing the integer in a file will take some amount of space. If I convert it into an iterator using yield , can I store the iterator in a

Re-structuring a list of Python Dicts using setdefault

柔情痞子 提交于 2019-12-24 12:47:58
问题 I am trying to re-structure a list of Python dictionaries by 'grouping' (that's probably not the correct expression, but using it as a proxy based on SQL) the dictionaries based on a (non-unique) value. I have got close, however I'm falling at the final hurdle, in that I couldn't work out how to re-assign each value to a name (i.e. I end up with what looks like a tuple rather than a dict). Additionally I have a superfluous list (i.e. my output ends up as [[{...}]] rather than [{...}]. I have

Can I make an iterator with a simple function? (No generator or Symbol.iterator)

☆樱花仙子☆ 提交于 2019-12-24 07:25:32
问题 I have been trying to make an iterator using a plain function, without a generator or using the Symbol.iterator protocol for academic purposes. For that, I have made a function that returns an object with a next parameter, but trying to run it as the iterable argument of an for...of loop yields unwanted results. Here is my code so far, which I copied from the Iterators and Generators page on MDN: function iterateThis(arr){ let i = 0; return { next: function() { return i < arr.length ? {value:

Can I make an iterator with a simple function? (No generator or Symbol.iterator)

谁说胖子不能爱 提交于 2019-12-24 07:25:20
问题 I have been trying to make an iterator using a plain function, without a generator or using the Symbol.iterator protocol for academic purposes. For that, I have made a function that returns an object with a next parameter, but trying to run it as the iterable argument of an for...of loop yields unwanted results. Here is my code so far, which I copied from the Iterators and Generators page on MDN: function iterateThis(arr){ let i = 0; return { next: function() { return i < arr.length ? {value:

python object not iterable error in function

我们两清 提交于 2019-12-24 03:53:26
问题 I have a simple function with the following comdList = range(0,27) for t, in comdList: print t However it returns a in object not iterable error outside the function it works fine. Whats going on?? 回答1: Try this: for t in comdList: print t The extra comma after the t variable was causing the error, because of it Python thinks that the iterable is going to return a sequence of 1-tuples to unpack - for example: ((1,), (2,)) but instead it received an iterable of single elements. 来源: https:/

Separating the __iter__ and __next__ methods

一世执手 提交于 2019-12-24 01:58:10
问题 In Python 3, it is standard procedure to make a class an iterable and iterator at the same time by defining both the __iter__ and __next__ methods. But I have problems to wrap my head around this. Take this example which creates an iterator that produces only even numbers: class EvenNumbers: def __init__(self, max_): self.max_ = max_ def __iter__(self): self.n = 0 return self def __next__(self): if self.n <= self.max: result = 2 * self.n self.n += 1 return result raise StopIteration instance

How to print out individual Strings from Iterable<String>

主宰稳场 提交于 2019-12-23 12:42:02
问题 I am having trouble printing out individual Strings from Interable object. I have this given function prefixMatch(String someword) that returns Iterable (that keeps a LinkedList of Strings, I think). I have tried turning it into a list but it wont work. Dose anyone know how to get the Strings out of it one by one? tst is a Ternary search tree Iterable<String> words = tst.prefixMatch(word); 回答1: If it's Iterable you can do an extended for on it. Iterable<String> iterable; for(String s :

Are there any Java standard classes that implement Iterable without implementing Collection?

夙愿已清 提交于 2019-12-23 06:48:24
问题 I have a conundrum that's caused me to ponder whether there are any standard java classes that implement Iterable<T> without also implementing Collection<T> . I'm implementing one interface that requires me to define a method that accepts an Iterable<T> , but the object I'm using to back this method requires a Collection<T> . This has me doing some really kludgy feeling code that give some unchecked warnings when compiled. public ImmutableMap<Integer, Optional<Site>> loadAll( Iterable<?

Iterators in Python 3

ε祈祈猫儿з 提交于 2019-12-22 09:29:45
问题 In Python 3 a lot of functions (now classes) that returned lists now return iterables, the most popular example being range . In this case range was made an iterable in Python 3 to improve performance, and memory efficiency (since you don't have to build a list anymore). Other "new" iterables are map , enumerate , zip and the output of the dictionary operations dict.keys() , dict.values() and dict.items() . (There are probably more, but I don't know them). Some of them ( enumerate and map )