Number Sequencing, Grouping and Filtering

F

flebber

Hi

I was hoping someone would be able to point me in the direction of
some good documentation regarding sequencing, grouping and filtering
and in which order they should be done.

As a small example it is easy to create the range of numbers 1 to 20.
But if I wanted to group all possible combinations of sets of 4
numbers within this range is there already an in uilt function for
this I am searching the module docs with "number sequencing" and
"number grouping" but cannot find info.

Then if I wanted to refine this results further eg no consecutive
numbers to be contained in sets. Is it best to create all sets and
then filter the sets for things matching this criteria or to set this
condition in a creation. Creating sets and then filtering would soon
become unwieldy with a larger range I would imagine..

An ideas, pointers to docs or better search terms to help me explore
this further would be appreciated.

Thanks Sayth
 
F

flebber

Hi

I was hoping someone would be able to point me in the direction of
some good documentation regarding sequencing, grouping and filtering
and in which order they should be done.

As a small example it is easy to create the range of numbers 1 to 20.
But if I wanted to group all possible combinations of sets of 4
numbers within this range is there already an in uilt function for
this I am searching the module docs with "number sequencing" and
"number grouping" but cannot find info.

Then if I wanted to refine this results further eg no consecutive
numbers to be contained in sets. Is it best to create all sets and
then filter the sets for things matching this criteria or to set this
condition in a creation. Creating sets and then filtering would soon
become unwieldy with a larger range I would imagine..

An ideas, pointers to docs or better search terms to help me explore
this further would be appreciated.

Thanks Sayth

I have just found itertools is this acheivable using combinations()
and groupby() in itertools?
 
C

Chris Rebert

Hi

I was hoping someone would be able to point me in the direction of
some good documentation regarding sequencing, grouping and filtering
and in which order they should be done.

As a small example it is easy to create the range of numbers 1 to 20.
But if I wanted to group all possible combinations of sets of 4
numbers within this range is there already an in uilt function for
this I am searching the module docs with "number sequencing" and
"number grouping" but cannot find info.

I think itertools (http://docs.python.org/library/itertools.html)
combined with list comprehensions
(http://docs.python.org/dev/tutorial/datastructures.html#list-comprehensions)
should be able to accomplish what you want.

Cheers,
Chris
 
C

Chris Rebert

I have just found itertools is this acheivable using combinations()
and groupby() in itertools?

Yes; indeed, those were the functions your post brought to mind and
which led me to suggest itertools.

Cheers,
Chris
 
F

flebber

Yes; indeed, those were the functions your post brought to mind and
which led me to suggest itertools.

Cheers,
Chris

the only issue i can see is that i am using python 2.54 currently as
ifelt it more supported by other programs than 2.6 or 3.0. After
searching it seems that itertools has had a upgrade in 2.61
 
R

Raymond Hettinger

[flebber]
the only issue i can see is that i am using python 2.54 currently as
ifelt it more supported by other programs than 2.6 or 3.0. After
searching it seems that itertools has had a upgrade in 2.61

All of the itertools include pure python equivalents in their docs,
so it should be possible to backport them to 2.5 by cutting and
pasting the code equivalent.


Raymond

-------------
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
indices = range(r)
yield tuple(pool for i in indices)
while 1:
for i in reversed(range(r)):
if indices != i + n - r:
break
else:
return
indices += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool for i in indices)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top