iterable

Chain dynamic iterable of context managers to a single with statement

馋奶兔 提交于 2019-12-01 22:18:01
I have a bunch of context managers that I want to chain. On the first glance, contextlib.nested looked like a fitting solution. However, this method is flagged as deprecated in the documentation which also states that the latest with statement allows this directly: Deprecated since version 2.7: The with-statement now supports this functionality directly (without the confusing error prone quirks). However I could not get Python 3.4.3 to use a dynamic iterable of context managers: class Foo(): def __enter__(self): print('entering:', self.name) return self def __exit__(self, *_): pass def __init_

Making objects iterable?

假装没事ソ 提交于 2019-12-01 20:24:23
问题 I'm trying to iterate over each row in a list of lists, append an element from each row to a new list, then find the unique elements in the new list. I understand that I can do this easily with a for loop. I'm trying a different route because I want to learn more about classes and functions. Here's an example of the list of lists. The first row is the header: legislators = [ ['last_name', 'first_name', 'birthday', 'gender', 'type', 'state', 'party'], ['Bassett', 'Richard', '1745-04-02', 'M',

“Can only iterable” Python error

寵の児 提交于 2019-12-01 13:54:49
Hello my fellow programmers. I am a fairly new programmer, and now I am facing a great predicament. I am getting the error: can only assign an iterable Firstly I don't know what that means. Secondly I will leave my code for you professionals to critique it: def num_top(int_lis): duplic_int_lis = int_lis int_firs= duplic_int_lis [0] int_lis[:] = duplic_int_lis [int_firs] Basically I am trying to find the [0] element in the list and then using that int as an index position to find the integer at that index position. int_lis[:] = duplic_int_lis [int_firs] means assign all the items of duplic_int

“Can only iterable” Python error

折月煮酒 提交于 2019-12-01 12:39:02
问题 Hello my fellow programmers. I am a fairly new programmer, and now I am facing a great predicament. I am getting the error: can only assign an iterable Firstly I don't know what that means. Secondly I will leave my code for you professionals to critique it: def num_top(int_lis): duplic_int_lis = int_lis int_firs= duplic_int_lis [0] int_lis[:] = duplic_int_lis [int_firs] Basically I am trying to find the [0] element in the list and then using that int as an index position to find the integer

Finding lowest value within a nested list?

此生再无相见时 提交于 2019-12-01 12:26:14
Im trying to write a function that takes a list and can print the lowest integer that is within that list. Now i'm trying to figure out what to do where this works with nested lists that if the lowest number is within one of those nested lists then overall it will print that number. My code is here: def listMin(): list2 = [3,4,[2,99,8],7] for i in range (len(list2)): if type(list2[i]) == type([]): y=min(i) list2.append(y) print "hello" if len(list2)== 0: return None else: x= min(list2) print x listMin() while this seems like it should print the number 2 it doesnt and just gives me an error

Java, Google Collections Library; problem with AbstractIterator?

*爱你&永不变心* 提交于 2019-12-01 09:09:33
问题 I am using the Google Collections library AbstractIterator to implement a generator. I ran across a problem while doing so; I've reduced it to a more basic type and reproduced the problem. This reduction is obviously overkill for what it does, counting from 1 to numelements via an Iterable. Essentially in the following code, the uncommented version works, and the commented one does not (provides a null element last, instead of ending on the last number). Am I doing something wrong, or is this

Caching a generator

吃可爱长大的小学妹 提交于 2019-12-01 05:14:53
A recent similar question ( isinstance(foo, types.GeneratorType) or inspect.isgenerator(foo)? ) got me curious about how to implement this generically. It seems like a generally-useful thing to have, actually, to have a generator-type object that will cache the first time through (like itertools.cycle ), report StopIteration, and then return items from the cache next time through, but if the object isn't a generator (i.e. a list or dict that inherently supports O(1) lookup), then don't cache, and have the same behaviour, but for the original list. Possibilities: 1) Modify itertools.cycle. It

Python filter / max combo - checking for empty iterator

五迷三道 提交于 2019-12-01 03:35:46
问题 (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 =

Does Rust have an equivalent to Python's list comprehension syntax?

*爱你&永不变心* 提交于 2019-12-01 02:29:48
Python list comprehension is really simple: >>> l = [x for x in range(1, 10) if x % 2 == 0] >>> [2, 4, 6, 8] Does Rust have an equivalent syntax like: let vector = vec![x for x in (1..10) if x % 2 == 0] // [2, 4, 6, 8] You can just use iterators: fn main() { let v1 = (0u32..9).filter(|x| x % 2 == 0).map(|x| x.pow(2)).collect::<Vec<_>>(); let v2 = (1..10).filter(|x| x % 2 == 0).collect::<Vec<u32>>(); println!("{:?}", v1); // [0, 4, 16, 36, 64] println!("{:?}", v2); // [2, 4, 6, 8] } Jacob Krall cute is a macro for Python-esque list and dictionary ( HashMap ) comprehensions in Rust. #[macro_use

Caching a generator

自作多情 提交于 2019-12-01 02:16:49
问题 A recent similar question (isinstance(foo, types.GeneratorType) or inspect.isgenerator(foo)?) got me curious about how to implement this generically. It seems like a generally-useful thing to have, actually, to have a generator-type object that will cache the first time through (like itertools.cycle ), report StopIteration, and then return items from the cache next time through, but if the object isn't a generator (i.e. a list or dict that inherently supports O(1) lookup), then don't cache,