iterable

What's the shortest way to count the number of items in a generator/iterator?

你离开我真会死。 提交于 2019-11-26 18:52:44
If I want the number of items in an iterable without caring about the elements themselves, what would be the pythonic way to get that? Right now, I would define def ilen(it): return sum(itertools.imap(lambda _: 1, it)) # or just map in Python 3 but I understand lambda is close to being considered harmful, and lambda _: 1 certainly isn't pretty. (The use case of this is counting the number of lines in a text file matching a regex, i.e. grep -c .) The usual way is sum(1 for i in it) Method that's meaningfully faster than sum(1 for i in it) when the iterable may be long (and not meaningfully

Length of generator output [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-26 18:43:32
This question already has an answer here: Getting number of elements in an iterator in Python 15 answers What's the shortest way to count the number of items in a generator/iterator? 6 answers Python provides a nice method for getting length of an eager iterable, len(x) that is. But I couldn't find anything similar for lazy iterables represented by generator comprehensions and functions. Of course, it is not hard to write something like: def iterlen(x): n = 0 try: while True: next(x) n += 1 except StopIteration: pass return n But I can't get rid of a feeling that I'm reimplementing a bicycle.

Kotlin's Iterable and Sequence look exactly same. Why are two types required?

安稳与你 提交于 2019-11-26 18:38:15
Both of these interfaces define only one method public operator fun iterator(): Iterator<T> Documentation says Sequence is meant to be lazy. But isn't Iterable lazy too (unless backed by a Collection )? The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T> . For Sequence<T> , the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { ... } returns another Sequence<R> and does not actually process the items until a terminal operation like

Why does Stream<T> not implement Iterable<T>?

随声附和 提交于 2019-11-26 17:02:27
In Java 8 we have the class Stream<T> , which curiously have a method Iterator<T> iterator() So you would expect it to implement interface Iterable<T> , which requires exactly this method, but that's not the case. When I want to iterate over a Stream using a foreach loop, I have to do something like public static Iterable<T> getIterable(Stream<T> s) { return new Iterable<T> { @Override public Iterator<T> iterator() { return s.iterator(); } }; } for (T element : getIterable(s)) { ... } Am I missing something here? There are already people asked the same on the mailing list ☺. The main reason is

Star * operator on left vs right side of an assignment statement

我是研究僧i 提交于 2019-11-26 16:54:01
问题 This questions stems from PEP 448 -- Additional Unpacking Generalizations and is present in Python 3.5 as far as I'm aware (and not back-ported to 2.x ). Specifically, in the section Disadvantages , the following is noted: Whilst *elements, = iterable causes elements to be a list , elements = *iterable , causes elements to be a tuple . The reason for this may confuse people unfamiliar with the construct. Which does indeed hold, for iterable = [1, 2, 3, 4] , the first case yields a list : >>>

Why is Java&#39;s Iterator not an Iterable?

为君一笑 提交于 2019-11-26 11:39:44
Why does the Iterator interface not extend Iterable ? The iterator() method could simply return this . Is it on purpose or just an oversight of Java's designers? It would be convenient to be able to use a for-each loop with iterators like this: for(Object o : someContainer.listSomeObjects()) { .... } where listSomeObjects() returns an iterator. PaulJWilliams Because an iterator generally points to a single instance in a collection. Iterable implies that one may obtain an iterator from an object to traverse over its elements - and there's no need to iterate over a single instance, which is what

Java: Get first item from a collection

半城伤御伤魂 提交于 2019-11-26 10:12:37
问题 If I have a collection, such as Collection<String> strs , how can I get the first item out? I could just call an Iterator , take its first next() , then throw the Iterator away. Is there a less wasteful way to do it? 回答1: Iterables.get(yourC, indexYouWant) Because really, if you're using Collections, you should be using Google Collections. 回答2: Looks like that is the best way to do it: String first = strs.iterator().next(); Great question... At first, it seems like an oversight for the

Convert ES6 Iterable to Array

社会主义新天地 提交于 2019-11-26 09:38:26
问题 Say you have an array-like Javascript ES6 Iterable that you know in advance will be finite in length, what\'s the best way to convert that to a Javascript Array? The reason for doing so is that many js libraries such as underscore and lodash only support Arrays, so if you wish to use any of their functions on an Iterable, it must first be converted to an Array. In python you can just use the list() function. Is there an equivalent in ES6? 回答1: You can use Array.from or the spread operator.

How does a Python for loop with iterable work? (`for party in feed.entry`)

早过忘川 提交于 2019-11-26 09:04:45
问题 What does for party in feed.entry signify and how does this for-loop actually work? for party in feed.entry: print party.location.address.text (I am used to C++ style for-loops, but the Python loops have left me confused.) 回答1: feed.entry is property of feed and it's value is (if it's not, this code will fail) object implementing iteration protocol (array, for example) and has iter method, which returns iterator object Iterator has next() method, returning next element or raising exception,

What&#39;s the shortest way to count the number of items in a generator/iterator?

放肆的年华 提交于 2019-11-26 06:38:38
问题 If I want the number of items in an iterable without caring about the elements themselves, what would be the pythonic way to get that? Right now, I would define def ilen(it): return sum(itertools.imap(lambda _: 1, it)) # or just map in Python 3 but I understand lambda is close to being considered harmful, and lambda _: 1 certainly isn\'t pretty. (The use case of this is counting the number of lines in a text file matching a regex, i.e. grep -c .) 回答1: The usual way is sum(1 for i in it) 回答2: