Itertools wishlists

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Mar 13 08:15:51 EST 2005


Thank you for your very good and interesting answer Raymond. In the
Itertool library there are functions not really simple to use/remember,
but a flatten() and a partition() can probably be easy to remember
(your window() function is probably a sliding window, so it's not a
partition(), I presume).


>Also, the solutions to that problem make the resulting function more
difficult to learn, remember, review, etc.<

I agree, the basic flatten function can be easy (in Mathematica there
isn't the difference between tuples and lists, and you cannot flatten a
string), but it can become less simple as an useful implementation for
Python. This was my suggestion for a possible flatten():

flatten(sequence, level=-1, tuples=True, strings=False, safe=False)
- tuples=True then it flattens tuples too.
- strings=True then it flattens strings with len(s)>1 too.
- safe if True it cheeks (with something like an iterative isrecursive)
for
  recursive references inside the sequence.
- level allows to specify the mapping level:
  level=0 no flattening.
  level=1 the flattening is applied to the first level only.
  level=2 the flattening is applied to the first and second level only.
  level=m where m>=actual depth. This is as level=-1.
  Etc.
  And like in the indexing of lists:
  level=-1 or None (default) means the flattening is applied up to the
leaves.
  level=-2 flattens up to pre-leaves.
  Etc.


>The nature of flattening is such that a C implementation doesn't offer
any special advantage over the various competing pure python versions.<

Even a well tuned (non recursive) standard python version can be okay,
it avoids people to design lots of slower\wrong functions by
themselves.


>And, there is also the issue of use cases. It appears to be much more
fun to toy around with developing flatten() recipes than it is to work
on applications that require it.<

It's not easy to define "require" because usually there are many ways
to solve every problem.

There are two situations that I've found can make use of the
flatten/partition, but you can probably find better ways to do the same
thing (and I can appreciate suggestions):

1)
The function f returns two values, but you need a flat list as result:
def f(x): return x, x**2
r = flatten( f(i) for i in range(10) )
print r

Alternative:
def f(x): return x, x**2
r = []
for i in range(10): r.extend( f(i) )
print r
(You can also use two append())


2)
A file with columns of numbers separated by a space/tab:
n n n n
n n n n
n n n n
...

ll = open("namefile").read().splitlines()
r = [map(float, l.split()) for l in ll]

Alternative:
ll = open("namefile").read().split()
r = partition(map(float, ll), 4)

This second version can be a little faster, but it has the disadvantage
that the column number 4 is hard coded.


>We likely need some other module for reduction functions like any(),
all(), no(), quantify(), take(), etc.<

Okay.


Your itertools.consume() looks like the Mathematica Scan[] function.

Bye,
Bearophile

[Remove HUGS if you want to mail me directly]




More information about the Python-list mailing list