iterable

Python threading error - must be an iterable, not int

◇◆丶佛笑我妖孽 提交于 2019-11-30 18:27:53
问题 I'm trying to calculate rolling r-squared of regression among first column and other columns in a dataframe (first column and second, first column and third etc.) But when I try threading, it kept telling me the error that TypeError: ParallelRegression() argument after * must be an iterable, not int". I'm wondering how do I fix this? Thanks very much! import threading totalThreads=3 #three different colors def ParallelRegression(threadnum): for i in range(threadnum): res[:,i]=sm.OLS(df.iloc[:

How can I make my class iterable so I can use foreach syntax?

三世轮回 提交于 2019-11-30 17:16:47
I have Book and BookList classes. BookList is something like this: public class BookList { private final List<Book> bList = new ArrayList<Book>(); public int size() { return bList.size(); } public boolean isEmpty() { ... } public boolean contains(Book b) { ... } public boolean add(Book b) { ... } public boolean remove(Book b) { .. } public void clear() { ... } public Object get(int index) { ... } } In my main class I want to print titles of books with in a for each loop: for(Book b : bList) { b.print(); } Eclipse says: Can only iterate over an array or an instance of java.lang.Iterable How can

Why there is no getFirst(iterable) method?

ε祈祈猫儿з 提交于 2019-11-30 17:11:21
Iterables present two methods for getLast public static <T> T getLast(Iterable<T> iterable); public static <T> T getLast(Iterable<T> iterable, @Nullable T defaultValue); but only one for getFirst public static <T> T getFirst(Iterable<T> iterable, @Nullable T defaultValue); Is there are any design/implementation reason for breaking symmetry? John B I think the point is that there is no reason for a getFirst(iterable) in that this could be done with iterable.iterator().next() . Guava makes an excellent attempt to keep the API small and so does not add things that could / should be done easily

How to perform Stream functions on an Iterable? [duplicate]

岁酱吖の 提交于 2019-11-30 05:43:13
This question already has an answer here: Why does Iterable<T> not provide stream() and parallelStream() methods? 3 answers In Java 8, the Stream class does not have any method to wrap a an Iterable . Instead, I am obtaining the Spliterator from the Iterable and then obtaining a Stream from StreamSupport like this: boolean parallel = true; StreamSupport.stream(spliterator(), parallel) .filter(Row::isEmpty) .collect(Collectors.toList()) .forEach(this::deleteRow); Is there some other way of generating Stream operations on an Iterable that I am missing? My similar question got marked as duplicate

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

ぐ巨炮叔叔 提交于 2019-11-30 04:15:20
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? 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 collection or an arbitrary element from an unordered collection. Raise StopIteration if the collection is empty. '''

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

梦想与她 提交于 2019-11-30 04:09:15
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.hasNext(); /* NOOP */) { O o = iter.next(); // Do something } However, if I do not have an iterable in the first place, but only an iterator (say, because a class

Get size of an Iterable in Java

吃可爱长大的小学妹 提交于 2019-11-29 23:26:08
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? TL;DR: Use the utility method Iterables.size(Iterable) of the great Guava library. Of your two code snippets, you should use the first one,

How can I make my class iterable so I can use foreach syntax?

泪湿孤枕 提交于 2019-11-29 16:09:20
问题 I have Book and BookList classes. BookList is something like this: public class BookList { private final List<Book> bList = new ArrayList<Book>(); public int size() { return bList.size(); } public boolean isEmpty() { ... } public boolean contains(Book b) { ... } public boolean add(Book b) { ... } public boolean remove(Book b) { .. } public void clear() { ... } public Object get(int index) { ... } } In my main class I want to print titles of books with in a for each loop: for(Book b : bList) {

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

半世苍凉 提交于 2019-11-29 13:16:28
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 ? 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 n == 's'][3] 7 If you didn't want to store the indices for each occurrence, or wanted to work with

“Int” object is not iterable

扶醉桌前 提交于 2019-11-29 11:59:39
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 + ororkulifeyrir * aldurstengd_ororka([age]) So my intention is to run through aldurstengd_ororka, and for each