iterable

How to find the index of the nth time an item appears in a list?

时光怂恿深爱的人放手 提交于 2019-11-27 07:46:03
问题 Given: x = ['w', 'e', 's', 's', 's', 'z','z', 's'] Each occurrence of s appears at the following indices: 1st: 2 2nd: 3 3rd: 4 4th: 7 If I do x.index('s') I will get the 1st index. How do I get the index of the 4th s ? 回答1: Using list comprehension and enumerate: >>> x = [ 'w', 'e', 's', 's', 's', 'z','z', 's'] >>> [i for i, n in enumerate(x) if n == 's'][0] 2 >>> [i for i, n in enumerate(x) if n == 's'][1] 3 >>> [i for i, n in enumerate(x) if n == 's'][2] 4 >>> [i for i, n in enumerate(x) if

Python - TypeError: 'int' object is not iterable

北战南征 提交于 2019-11-27 06:27:16
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 number1 == 8: cow = range(10000000,100000000) elif number1 == 9: cow = range(100000000,1000000000) elif

Java: Get first item from a collection

删除回忆录丶 提交于 2019-11-27 06:11:41
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? Carl Iterables.get(yourC, indexYouWant) Because really, if you're using Collections, you should be using Google Collections. 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 Collection interface. Note that "first" won't always return the first thing you put in the collection, and may

Check if a variable is iterable?

你说的曾经没有我的故事 提交于 2019-11-27 05:36:16
问题 Is there any way to check if an arbitrary variable type is iterable? So to check if it has indexed elements or I can actually loop over it's children? (Use foreach for example?) Is it possible to create a universal template for that? I've found techniques for other programming languages while searching for it. Yet still have to find out how to do this in C++. 回答1: It depends on what you mean by "iterable". It is a loose concept in C++ since you could implement iterators in many different ways

Convert Java Array to Iterable

谁说我不能喝 提交于 2019-11-27 04:15:47
问题 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

Convert ES6 Iterable to Array

时光总嘲笑我的痴心妄想 提交于 2019-11-27 01:22:54
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? You can use Array.from or the spread operator . Example: let x = new Set([ 1, 2, 3, 4 ]); let y = Array.from(x); console.log(y); // = [ 1, 2, 3, 4 ] let z = [

Suppressing treatment of string as iterable

落花浮王杯 提交于 2019-11-26 23:20:16
问题 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(

Using Objects in For Of Loops

落爺英雄遲暮 提交于 2019-11-26 22:04:25
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) } 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]; } You can use this syntax: let myObject = {first: "one"}; for(let [key, value] of Object.entries(myObject)) { console.log(key, value); // "first", "one" } However, Object.entries has poor support right now does not

Java: why can't iterate over an iterator?

≡放荡痞女 提交于 2019-11-26 21:09:32
问题 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); } } 回答1: but I still don't understand why this [...] was not made possible. I can see

How to make a custom object iterable?

落花浮王杯 提交于 2019-11-26 20:57:49
问题 I have a list of custom-class objects (sample is below). Using: list(itertools.chain.from_iterable(myBigList)) I wanted to "merge" all of the stations sublists into one big list. So I thought I need to make my custom class an iterable. Here is a sample of my custom class. class direction(object) : def __init__(self, id) : self.id = id self.__stations = list() def __iter__(self): self.__i = 0 # iterable current item return iter(self.__stations) def __next__(self): if self.__i<len(self._