iterable

“Can only join an iterable” python error

五迷三道 提交于 2019-12-05 16:03:57
问题 I've already looked at this post about iterable python errors: "Can only iterable" Python error But that was about the error "cannot assign an iterable". My question is why is python telling me: "list.py", line 6, in <module> reversedlist = ' '.join(toberlist1) TypeError: can only join an iterable I don't know what I am doing wrong! I was following this thread: Reverse word order of a string with no str.split() allowed and specifically this answer: >>> s = 'This is a string to try' >>> r = s

Python filter / max combo - checking for empty iterator

拟墨画扇 提交于 2019-12-05 09:58:47
(Using Python 3.1) I know this question has been asked many times for the general question of testing if iterator is empty; obviously, there's no neat solution to that (I guess for a reason - an iterator doesn't really know if it's empty until it's asked to return its next value). I have a specific example, however, and was hoping I can make clean and Pythonic code out of it: #lst is an arbitrary iterable #f must return the smallest non-zero element, or return None if empty def f(lst): flt = filter(lambda x : x is not None and x != 0, lst) if # somehow check that flt is empty return None

Scala Iterable Memory Leaks

让人想犯罪 __ 提交于 2019-12-05 07:46:42
I recently started playing with Scala and ran across the following. Below are 4 different ways to iterate through the lines of a file, do some stuff, and write the result to another file. Some of these methods work as I would think (though using a lot of memory to do so) and some eat memory to no end. The idea was to wrap Scala's getLines Iterator as an Iterable. I don't care if it reads the file multiple times - that's what I expect it to do. Here's my repro code: class FileIterable(file: java.io.File) extends Iterable[String] { override def iterator = io.Source.fromFile(file).getLines } //

What is the 2nd argument for the iter function in Python?

帅比萌擦擦* 提交于 2019-12-05 06:54:05
问题 Let's consider a file: $ echo -e """This is a foo bar sentence .\nAnd this is the first txtfile in the corpus .""" > test.txt $ cat test.txt This is a foo bar sentence . And this is the first txtfile in the corpus . And when I want to read the file by character, I can do https://stackoverflow.com/a/25071590/610569: >>> fin = open('test.txt') >>> while fin.read(1): ... fin.seek(-1,1) ... print fin.read(1), ... T h i s i s a f o o b a r s e n t e n c e . A n d t h i s i s t h e f i r s t t x t

python make class iterable by returning embedded iterable

被刻印的时光 ゝ 提交于 2019-12-05 00:10:19
I have a class in python, which has an iterable as instance variable. I want to iterate the instances of the class by iterating over the embedded iterable. I implemented this as follows: def __iter__(self): return self._iterable.__iter__() I don't really feel that comfortable calling the __iter__() method on the iterable, as it is a special method. Is this how you would solve this problem in python or is there a more elegant solution? The "best" way to way to delegate __iter__ would be: def __iter__(self): return iter(self._iterable) Alternately, it might be worth knowing about: def __iter__

Iterable to ArrayList elements change

旧街凉风 提交于 2019-12-04 20:26:56
So I am implementing a mapreduce job which means I am dealing with key value pairs. I have the variable Iterable<FreqDataWritable> values FreqDataWritable is an object that contains pieces of information, but for now I am only concerned with one piece of information it holds which is a String which is accessed by getFilename(). I have the following loop: ArrayList<String> filenames = new ArrayList<String>(); for(FreqDataWritable i : values) { filenames.add(i.getFilename()); } Now all I want to do is print the values in the array list filenames. for(int i = 0; i < filenames.size(); i++) {

How to implement “next” for a dictionary object to be iterable?

落爺英雄遲暮 提交于 2019-12-04 19:43:14
I've got the following wrapper for a dictionary: class MyDict: def __init__(self): self.container = {} def __setitem__(self, key, value): self.container[key] = value def __getitem__(self, key): return self.container[key] def __iter__(self): return self def next(self): pass dic = MyDict() dic['a'] = 1 dic['b'] = 2 for key in dic: print key My problem is that I don't know how to implement the next method to make MyDict iterable. Any advice would be appreciated. Dictionaries are themselves not an iterator (which can only be iterated over once ). You usually make them an iterable , an object for

Best way to convert Iterable<Character> to String?

一曲冷凌霜 提交于 2019-12-04 16:19:45
What's the easiest/fastest way to convert an Iterable<Character> to a String of the characters from the Iterable? For example, how to convert an Iterable of "A" , "B" , and "C" , to a String "ABC" ? The iter.toString() returns a String "[A, B, C]" . 5 Versions: 1) Java 8 streams collecting via StringBuilder.append() : public static String streamAppend(Iterable<Character> chars){ return StreamSupport.stream(chars.spliterator(), true) .collect( StringBuilder::new, StringBuilder::append, StringBuilder::append ) .toString(); } Java 8 Streams collecting via Collectors.joining() public static String

Iterators to read and process file in Java

随声附和 提交于 2019-12-04 06:52:36
Suppose I have a class Point and a function to process Point instances class Point { private final int x, y; ... } ... void handlePoints(Iterable<Point> points) { for (Point p: points) {...} } Now I would like to read points from a file. Each line of the file contains two numbers, so I have a function ("factory method") to create a point from a line. Point makePoint(String line) { ... } What should I do now? I can write a function to read the file to a list of points and call the handlePoints function. List<Point> readPoints(BufferedReader reader) {...} // use makePoint here void handlePoints

numpy.float64 is not iterable

故事扮演 提交于 2019-12-04 06:24:20
问题 I'm trying to print a function which uses several parameters from numpy array's and lists, but I keep getting the error "numpy.float 64 object is not iterable". I've looked at several questions on the forum about this topic and tried different answers but none seem to work (or I might be doing something wrong I'm still a beginner at python) but it all comes down to the same thing, I'm stuck and I hope you guys can help. I'm using python 2.7, this is the code: EDIT: Included the error message