iterable

Elegant way to skip elements in an iterable

不打扰是莪最后的温柔 提交于 2019-12-18 06:40:52
问题 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

Elegant way to skip elements in an iterable

爱⌒轻易说出口 提交于 2019-12-18 06:37:38
问题 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

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

蹲街弑〆低调 提交于 2019-12-18 04:54:11
问题 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(),

Is file object in python an iterable

淺唱寂寞╮ 提交于 2019-12-18 02:48:09
问题 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? 回答1: It is not only an

Is file object in python an iterable

无人久伴 提交于 2019-12-18 02:47:43
问题 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? 回答1: It is not only an

Implement Java Iterator and Iterable in same class?

情到浓时终转凉″ 提交于 2019-12-17 22:25:44
问题 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;

Why aren't Enumerations Iterable?

£可爱£侵袭症+ 提交于 2019-12-17 16:31:43
问题 In Java 5 and above you have the foreach loop, which works magically on anything that implements Iterable : for (Object o : list) { doStuff(o); } However, Enumerable still does not implement Iterable , meaning that to iterate over an Enumeration you must do the following: for(; e.hasMoreElements() ;) { doStuff(e.nextElement()); } Does anyone know if there is a reason why Enumeration still does not implement Iterable ? Edit: As a clarification, I'm not talking about the language concept of an

Python - TypeError: 'int' object is not iterable

空扰寡人 提交于 2019-12-17 06:26:20
问题 Here's my code: import math print "Hey, lets solve Task 4 :)" number1 = input ("How many digits do you want to look at? ") number2 = input ("What would you like the digits to add up to? ") if number1 == 1: cow = range(0,10) elif number1 == 2: cow = range(10,100) elif number1 == 3: cow = range(100,1000) elif number1 == 4: cow = range(1000,10000) elif number1 == 5: cow = range(10000,100000) elif number1 == 6: cow = range(100000,1000000) elif number1 == 7: cow = range(1000000,10000000) elif

Why is the iterator method present in both Iterable and Collection interfaces? [duplicate]

試著忘記壹切 提交于 2019-12-13 11:45:52
问题 This question already has an answer here : Method iterator() declared in java.util.Collection and in java.lang.Iterable, its superinterface? (1 answer) Closed last year . The Iterable interface has the method below: Iterator<T> iterator(); The Collection interface extends Iterable , and it also declares the same method. I am doubtful what was the need of putting the same method declaration twice while designing Java collections? 回答1: The main reason is Java 5. java.util.Collection with its

How to implements Iterable

て烟熏妆下的殇ゞ 提交于 2019-12-13 08:59:07
问题 In my program, I write my own LinkedList class. And an instance, llist. To use it in foreach loop as following, LinkedList needs to implement Iterable? for(Node node : llist) { System.out.print(node.getData() + " "); } Here following is my LinkedList class. Please let me know how can I make it Iterable? public class LinkedList implements Iterable { private Node head = null; private int length = 0; public LinkedList() { this.head = null; this.length = 0; } LinkedList (Node head) { this.head =