I'm trying to generate the morris sequence in python. My current solution is below, but I feel like I just wrote c in python. Can anyone provide a more pythonic solution?
def morris(x):
a = ['1', '11']
yield a[0]
yield a[1]
while len(a) <= x:
s = ''
count = 1
al = a[-1]
for i in range(0,le...
I have a list of items (which are HTML table rows, extracted with Beautiful Soup) and I need to iterate over the list and get even and odd elements (I mean index) for each loop run.
My code looks like this:
for top, bottom in izip(table[::2], table[1::2]):
#do something with top
#do something else with bottom
How to make this...
in my views,
if i import an itertools module:
from itertools import chain
and i chain some objects with it:
franktags = Frank.objects.order_by('date_added').reverse().filter(topic__exact='art')
amytags = Amy.objects.order_by('date_added').reverse().filter(topic__exact='art')
timtags = Tim.objects.order_by('date_added').reverse().fi...
in my django view, if i import operator, and use the following code:
multitags = sorted(multitags, key=operator.attrgetter('date_added'))
is there an easy way to reverse the order – such that i get the dates in descending order
(today at top; last week underneath)?
...
if my views code is:
arttags = sorted(arttags, key=operator.attrgetter('date_added'), reverse=True)
what is the argument that will limit the result to 50 tags?
I'm assuming this:
.... limit=50)
is incorrect.
more complete code follows:
videoarttags = Media.objects.order_by('date_added'),filter(topic__exact='art')
audioarttags =...
Question ago (http://stackoverflow.com/questions/1271320/reseting-generator-object-in-python) I was recommended to use itertools.tee. Actually I'm using IronPython, in the library we can see many usage of this feature, but there is no implementation (in *.py). That is why I'm confusing how to include this package to my c# project?
...
I'm searching for a library (preferably generic) that generates iterable combinations and permutations of data contained in collections. Cartesian product would also be nice.
The best way of describing what I want would be "itertools for Java".
...
I have 2 dimensional list created at runtime (the number of entries in either dimension is unknown). For example:
long_list = [ [2, 3, 6], [3, 7, 9] ]
I want to iterate through it by getting the ith entry from each list inside the long_list:
for entry in long_list.iter():
#entry will be [2, 3] then [3, 7] then [6, 9]
I know tha...
Original question: Can someone tell me how to use "slice lists" and the "ellipsis"? When are they useful? Thanks.
Here's what the language definition says about "slice_list" and "ellipsis"; Alex Martelli's answer points out their origin, which is not what I had envisioned.
[http://docs.python.org/reference/expressions.html#tok-slici...
I have a list of lists (generated with a simple list comprehension):
>>> base_lists = [[a, b] for a in range(1, 3) for b in range(1, 6)]
>>> base_lists
[[1,1],[1,2],[1,3],[1,4],[1,5],[2,1],[2,2],[2,3],[2,4],[2,5]]
I want to turn this entire list into a tuple containing all of the values in the lists, i.e.:
resulting_tuple = (1,1,1,2...
I want to get started trying to develop a few simple applications with PyObjC. I installed PyObjC and the Xcode templates. I know that PyObjC itself works, since I've run this script successfully. When I tried to create a project from the Cocoa-Python Application template and ran it, I got this error:
Traceback (most recent call last):
...
I'm trying to create a possible list of codons given a protein sequence.
Basically, the script i'm trying to create will process a given string input and outputs a possible combinations of another set of strings the input represents.
For example, the character 'F' represents either 'UUU' or 'UUC'; the character 'I' represents either 'A...
I'm looking for a way to "page through" a Python iterator. That is, I would like to wrap a given iterator iter and page_size with another iterator that would would return the items from iter as a series of "pages". Each page would itself be an iterator with up to page_size iterations.
I looked through itertools and the closest thing ...
Python's itertools module provides a lots of goodies with respect to processing an iterable/iterator by use of generators. For example,
permutations(range(3)) --> 012 021 102 120 201 210
combinations('ABCD', 2) --> AB AC AD BC BD CD
[list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
What are the equivalent in Ruby?
By eq...
I have a list of strings similar to this list:
tags = ('apples', 'apricots', 'oranges', 'pears', 'peaches')
How should I go about grouping this list by the first character in each string using itertools.groupby()? How should I supply the 'key' argument required by itertools.groupby()?
...
In python I have the following function:
def is_a_nice_element(element, parameter):
#do something
return True or False
Now I would like to filter a list with this function as predicate, giving a fixed parameter. Python has the itertools.ifilter function, but I can't figure out how to pass the parameter. Is this possible? If no...
I want to group by on dict key
>>> x
[{'a': 10, 'b': 90}, {'a': 20}, {'a': 30}, {'a': 10}]
>>> [(name, list(group)) for name, group in groupby(x, lambda p:p['a'])]
[(10, [{'a': 10, 'b': 90}]), (20, [{'a': 20}]), (30, [{'a': 30}]), (10, [{'a': 10}])]
This must group on key 10 :(
...
Hi everyone,
I have the following input:
input = [(dog, dog, cat, mouse), (cat, ruby, python, mouse)]
and trying to have the following output:
outputlist = [[0, 0, 1, 2], [1, 3, 4, 2]]
outputmapping = {0:dog, 1:cat, 2:mouse, 3:ruby, 4:python, 5:mouse}
Any tips on how to handle given with scalability in mind (var input can get rea...
I have a list which contains a number of things:
lista = ['a', 'b', 'foo', 'c', 'd', 'e', 'bar']
I'd like to get the first item in the list that fulfils a predicate, say len(item) > 2. Is there a neater way to do it than itertools' dropwhile and next?
first = next(itertools.dropwhile(lambda x: len(x) <= 2, lista))
I did use [item f...
I have a set of trajectories, made up of points along the trajectory, and with the coordinates associated with each point. I store these in a 3d array ( trajectory, point, param). I want to find the set of r trajectories that have the maximum accumulated distance between the possible pairwise combinations of these trajectories. My first ...