iterable

Elegant way to skip elements in an iterable

拜拜、爱过 提交于 2019-11-29 10:52:26
I've got a large iterable, in fact, a large iterable given by: itertools.permutations(range(10)) I would like to access to the millionth element. I alredy have problem solved in some different ways. Casting iterable to list and getting 1000000th element: return list(permutations(range(10)))[999999] Manually skiping elements till 999999: p = permutations(range(10)) for i in xrange(999999): p.next() return p.next() Manually skiping elements v2: p = permutations(range(10)) for i, element in enumerate(p): if i == 999999: return element Using islice from itertools: return islice(permutations(range

What exactly does “iterable” mean in Python? Why isn't my object which implements __getitem__() an iterable?

对着背影说爱祢 提交于 2019-11-29 07:13:41
First I want to clarify, I'm NOT asking what is "iterator". This is how the term "iterable" is defined in Python's doc : iterable An object capable of returning its members one at a time. Examples of iterables include all sequence types (such as list, str, and tuple) and some non-sequence types like dict, file objects, and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ...). When an iterable object is passed as an argument to the built-in function iter(), it

Shortest way to get first item of `OrderedDict` in Python 3

余生长醉 提交于 2019-11-29 02:07:07
问题 What's the shortest way to get first item of OrderedDict in Python 3? My best: list(ordered_dict.items())[0] Quite long and ugly. I can think of: next(iter(ordered_dict.items())) # Fixed, thanks Ashwini But it's not very self-describing. Any better suggestions? 回答1: Programming Practices for Readabililty In general, if you feel like code is not self-describing, the usual solution is to factor it out into a well-named function: def first(s): '''Return the first element from an ordered

Is file object in python an iterable

白昼怎懂夜的黑 提交于 2019-11-29 01:52:54
I have a file "test.txt": this is 1st line this is 2nd line this is 3rd line the following code lines = open("test.txt", 'r') for line in lines: print "loop 1:"+line for line in lines: print "loop 2:"+line only prints: loop 1:this is 1st line loop 1:this is 2nd line loop 1:this is 3rd line It doesn't print loop2 at all. Two questions: the file object returned by open(), is it an iterable? that's why it can be used in a for loop? why loop2 doesn't get printed at all? It is not only an iterable , it is an iterator , which is why it can only traverse the file once. You may reset the file cursor

Why does Java not allow foreach on iterators (only on iterables)? [duplicate]

和自甴很熟 提交于 2019-11-29 01:20:22
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why is Java's Iterator not an Iterable? Idiomatic way to use for-each loop given an iterator? Can we use for-each loop for iterating the objects of Iterator type? The foreach loop are as far as I know syntax sugar added in Java 5. So Iterable<O> iterable; for(O o : iterable) { // Do something } will essentially produce the same bytecode as Iterable<O> iterable; for(Iterator<O> iter = iterable.iterator(); iter

How do I make a class iterable?

这一生的挚爱 提交于 2019-11-29 01:19:51
This is my class public class csWordSimilarity { public int irColumn1 = 0; public int irColumn2 = 0; public int irColumn3 = 0; public int irColumn4 = 0; public int irColumn5 = 0; } I want to make that class iterable to be used like the way below foreach (int irVal in myVarWordSimilarity) { } myVarWordSimilarity is csWordSimilarity type. So I want to iterate all public int variables. How do I need to modify csWordSimilarity class for making it iterable like the way above. edvaldig You can implement IEnumerable and have the GetEnumerator override return an "iterator" over the variables using the

Implement Java Iterator and Iterable in same class?

拟墨画扇 提交于 2019-11-28 18:52:17
I am trying to understand Java Iterator and Iterable interfaces I am writing this class class MyClass implements Iterable<String> { public String[] a = null; public MyClass(String[] arr) { a = arr; } public MyClassIterator iterator() { return new MyClassIterator(this); } public class MyClassIterator implements Iterator<String> { private MyClass myclass = null; private int count = 0; public MyClassIterator(MyClass m) { myclass = m; } public boolean hasNext() { return count < myclass.a.length; } public String next() { int t = count; count++; return myclass.a[t]; } public void remove() { throw

Chart of IEnumerable LINQ equivalents in Scala? [duplicate]

一笑奈何 提交于 2019-11-28 15:09:55
Possible Duplicate: LINQ analogues in Scala I am looking for chart which shows equivalents in Scala of LINQ methods for IEnumerable: First is head Select is map SingleOrDefault is ... (I don't know) ... and so on Does anyone know anything of such "translate" table? missingfaktor I am only listing out the equivalents of functions from Enumerable<A> . This is incomplete as of now. I will try to update this later with more. xs.Aggregate(accumFunc) -> xs.reduceLeft(accumFunc) xs.Aggregate(seed, accumFunc) -> xs.foldLeft(seed)(accumFunc) xs.Aggregate(seed, accumFunc, trans) -> trans(xs.foldLeft

What is the technical definition of a Javascript iterable and how do you test for it?

一个人想着一个人 提交于 2019-11-28 08:45:16
问题 I've been implementing a useful subclass of the ES6 Set object. For many of my new methods, I want to accept an argument that can be either another Set or an Array, or really anything that I can iterate. I've been calling that an "iterable" in my interface and just use .forEach() on it (which works fine for a Set or an Array. Example code: // remove items in this set that are in the otherIterable // returns a count of number of items removed remove(otherIterable) { let cnt = 0; otherIterable

Implementing the Iterable interface

一曲冷凌霜 提交于 2019-11-28 08:06:40
问题 I just found this exam question in an old exam paper and am readying myself for an upcoming exam. I cannot figure it out : The following depicts a contrived partial class which implements the Iterable interface. The only purpose of this class is to provide a method to iterate over the attribute things. There are two things we need to fill in in the class to finish it. Here is the class private class PartialIterableClass /*FILL IN */ { private String[] things; public PartialIterableClass(