iterable

“Int” object is not iterable

时光怂恿深爱的人放手 提交于 2019-11-28 06:03:58
问题 I'm trying to run a for loop. Here's the section of my code I'm having trouble with: aldurstengd_ororka = {(18, 19, 20, 21, 22, 23, 24):1, (25):0.95, (26):0.90, (27):0.85, (28, 29):0.75, (30, 31):0.65, (32, 33):0.55, (34, 35):0.45, (36, 37):0.35, (40, 41, 42, 43, 44, 45):0.15, (46, 47, 48, 49, 50):0.10, (51, 52, 53, 54, 55):0.075, (56, 57, 58, 59, 60):0.05, (61, 62, 63, 64, 65, 66):0.025} for age in aldurstengd_ororka.keys(): for item in age: if ororkualdur == item: baetur = baetur +

What is the Iterable interface used for?

一笑奈何 提交于 2019-11-28 05:44:21
I am a beginner and I cannot understand the real effect of the Iterable interface. Besides what Jeremy said, its main benefit is that it has its own bit of syntactic sugar: the enhanced for-loop . If you have, say, an Iterable<String> , you can do: for (String str : myIterable) { ... } Nice and easy, isn't it? All the dirty work of creating the Iterator<String> , checking if it hasNext() , and calling str = getNext() is handled behind the scenes by the compiler. And since most collections either implement Iterable or have a view that returns one (such as Map 's keySet() or values() ), this

Suppressing treatment of string as iterable

こ雲淡風輕ζ 提交于 2019-11-27 23:36:54
UPDATE: An idea to make built-in strings non-iterable was proposed on python.org in 2006 . My question differs in that I'm trying to only suppress this features once in a while; still this whole thread is quite relevant. Here are the critical comments by Guido who implemented non-iterable str on a trial basis: [...] I implemented this (it was really simple to do) but then found I had to fix tons of places that iterate over strings. For example: The sre parser and compiler use things like set("0123456789") and also iterate over the characters of the input regexp to parse it. difflib has an API

Why aren't Enumerations Iterable?

被刻印的时光 ゝ 提交于 2019-11-27 23:24:30
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 enum , I'm talking a Java-specific class in the Java API called ' Enumeration '. Enumeration hasn't

Java: why can't iterate over an iterator?

核能气质少年 提交于 2019-11-27 22:47:13
I read Why is Java's Iterator not an Iterable? and Why aren't Enumerations Iterable? , but I still don't understand why this: void foo(Iterator<X> it) { for (X x : it) { bar(x); baz(x); } } was not made possible. In other words, unless I'm missing something, the above could have been nice and valid syntactic sugar for: void foo(Iterator<X> it) { for (X x; it.hasNext();) { x = it.next(); bar(x); baz(x); } } gustafc but I still don't understand why this [...] was not made possible. I can see several reasons: Iterator s are not reusable, so a for/each would consume the iterator - not incorrect

How do I make a class iterable?

好久不见. 提交于 2019-11-27 21:36:24
问题 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. 回答1: You can

Convert Java Array to Iterable

时光总嘲笑我的痴心妄想 提交于 2019-11-27 17:55:13
I have an Array of primitives, for example for int, int[] foo. It might be a small sized one, or not. int foo[] = {1,2,3,4,5,6,7,8,9,0}; What is the best way to create an Iterable<Integer> from it? Iterable<Integer> fooBar = convert(foo); Notes: Please do not answer using loops (unless you can give a good explanation on how the compiler do something smart about them?) Also note that int a[] = {1,2,3}; List<Integer> l = Arrays.asList(a); Will not even compile Type mismatch: cannot convert from List<int[]> to List<Integer> Also check Why is an array not assignable to Iterable? before answering.

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

a 夏天 提交于 2019-11-27 15:07:46
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 : >>> *elements, = iterable >>> elements [1, 2, 3, 4] While for the second case a tuple is created: >>>

Get size of an Iterable in Java

隐身守侯 提交于 2019-11-27 11:25:19
问题 I need to figure out the number of elements in an Iterable in Java. I know I can do this: Iterable values = ... it = values.iterator(); while (it.hasNext()) { it.next(); sum++; } I could also do something like this, because I do not need the objects in the Iterable any further: it = values.iterator(); while (it.hasNext()) { it.remove(); sum++; } A small scale benchmark did not show much performance difference, any comments or other ideas for this problem? 回答1: TL;DR: Use the utility method

Chart of IEnumerable LINQ equivalents in Scala? [duplicate]

徘徊边缘 提交于 2019-11-27 09:01:58
问题 This question already has answers here : Closed 7 years ago . 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? 回答1: 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