iterable

“yield from iterable” vs “return iter(iterable)”

徘徊边缘 提交于 2019-12-09 08:18:23
问题 When wrapping an (internal) iterator one often has to reroute the __iter__ method to the underlying iterable. Consider the following example: class FancyNewClass(collections.Iterable): def __init__(self): self._internal_iterable = [1,2,3,4,5] # ... # variant A def __iter__(self): return iter(self._internal_iterable) # variant B def __iter__(self): yield from self._internal_iterable Is there any significant difference between variant A and B? Variant A returns an iterator object that has been

Is there any official contract for the Iterable interface with respect to multiple usage?

一个人想着一个人 提交于 2019-12-09 05:22:50
问题 Since Java 5, we have the new java.lang.Iterable type that can be used in foreach loops as such: for (Object element : iterable); The Iterable contract does not specify whether its iterator() method can be called more than once before disposing of the Iterable. I.e., it is not clear whether the following can be expected to work for all Iterables : for (Object element : iterable); for (Object element : iterable); For instance, an Iterator wrapping implementation cannot be used twice: public

Why does len() not support iterators?

时光总嘲笑我的痴心妄想 提交于 2019-12-08 21:53:00
问题 Many of Python's built-in functions ( any() , all() , sum() to name some) take iterables but why does len() not? One could always use sum(1 for i in iterable) as an equivalent, but why is it len() does not take iterables in the first place? 回答1: Many iterables are defined by generator expressions which don't have a well defined len. Take the following which iterates forever: def sequence(i=0): while True: i+=1 yield i Basically, to have a well defined length, you need to know the entire

Django: TypeError: 'Like' object is not iterable (Ajax request)

折月煮酒 提交于 2019-12-08 07:41:16
问题 I am not getting a clear picture regarding the Model (Like) object is not iterable. Error liked, created = Like.objects.create(question=create_id) TypeError: 'Like' object is not iterable model.py class Question(models.Model): user = models.ForeignKey(User) title = models.CharField(max_length= 200) description = models.TextField() location = models.CharField(max_length=150) tags = TaggableManager() time = models.DateTimeField(auto_now_add=True) def __unicode__(self): return self.title class

ValueError: invalid literal for float() in Python

折月煮酒 提交于 2019-12-07 07:39:05
问题 To all: I have curious if someone can help me understand the error: ValueError: invalid literal for float(). I am getting this when I am passing a text file to a list then trying to convert this list to float values. a = open("input.txt","r") lines = a.readlines() b = map(float, lines) What is odd, at least to me is that when I process: print repr(lines[0]) I get: '0.000\t0.000...\t0.000\t0.000\n' and print type(lines[0]) I get: <type 'str'> I don't understand therefore why the map(float,

Python: map in place [duplicate]

风格不统一 提交于 2019-12-06 17:18:23
问题 This question already has answers here : Is there an in-place equivalent to 'map' in python? (4 answers) Closed 3 years ago . I was wondering if there is a way to run map on something. The way map works is it takes an iterable and applies a function to each item in that iterable producing a list. Is there a way to have map modify the iterable object itself? 回答1: A slice assignment is often ok if you need to modify a list in place mylist[:] = map(func, mylist) 回答2: It's simple enough to write:

Iterators to read and process file in Java

孤街醉人 提交于 2019-12-06 02:49:30
问题 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

Python __iter__ and for loops

别说谁变了你拦得住时间么 提交于 2019-12-06 01:55:28
As I understand it, I can use the for loop construction on an object with a __iter__ method that returns an iterator. I have an object for which I implement the following __getattribute__ method: def __getattribute__(self,name): if name in ["read","readlines","readline","seek","__iter__","closed","fileno","flush","mode","tell","truncate","write","writelines","xreadlines"]: return getattr(self.file,name) return object.__getattribute__(self,name) I have an object of this class, a for which the following happens: >>> hasattr(a,"__iter__") True >>> for l in a: print l ... Traceback (most recent

Finding groups of increasing numbers in a list

僤鯓⒐⒋嵵緔 提交于 2019-12-05 20:32:16
问题 The aim is to find groups of increasing/monotonic numbers given a list of integers. Each item in the resulting group must be of a +1 increment from the previous item Given an input: x = [7, 8, 9, 10, 6, 0, 1, 2, 3, 4, 5] I need to find groups of increasing numbers and achieve: increasing_numbers = [(7,8,9,10), (0,1,2,3,4,5)] And eventually also the number of increasing numbers: len(list(chain(*increasing_numbers))) And also the len of the groups: increasing_num_groups_length = [len(i) for i

java iterator/iterable subinterface

依然范特西╮ 提交于 2019-12-05 17:56:31
I have an interface for a variety of classes, all of which should implement Iterator, so I have something like public interface A extends Iterable<A> { ...otherMethods()... } For the concrete classes, however, this means I must use public class B implements A { public Iterator<A> iterator() {...} } when I'd prefer (or at least, think I'd prefer) to use public Iterator<B> iterator() {...} so that concrete use of the class could have the explicit type (in case I wanted methods that weren't in the interface to be available, or some such. Maybe that should never come up? Or it's poor design if it