a few extensions for the itertools

Mathias Panzenboeck e0427417 at student.tuwien.ac.at
Sun Nov 19 15:35:24 EST 2006


I wrote a few functions which IMHO are missing in python(s itertools).

You can download them here:
http://sourceforge.net/project/showfiles.php?group_id=165721&package_id=212104

A short description to all the functions:

icmp(iterable1, iterable2) -> integer
	Return negative if iterable1 < iterable2,
	zero if iterable1 == iterable1,
	positive if iterable1 > iterable1.

isum(iterable, start=0) -> value
	Returns the sum of the elements of a iterable
	plus the value of parameter 'start'.  When the
	iterable is empty, returns start.


iproduct(iterable, start=0) -> value
	Returns the product of the elements of a iterable
	times the value of parameter 'start'.  When the
	iterable is empty, returns start.


forall(predicate, iterable, default=True) -> bool
	Returns True, when for all elements x in iterable
	predicate(x) is True. When the iterable is empty,
	returns default.


forany(predicate, iterable, default=False) -> bool
	Returns True, when for any element x in iterable
	predicate(x) is True. When the iterable is empty,
	returns default.


take(n,iterable) -> iterator
	returns a iterator over the first n
	elements of the iterator


drop(n,iterable) -> iterable
	drops the first n elemetns of iterable and
	return a iterator over the rest


heads(iterable) -> iterator over all heads
	example:
	for head in heads([1,2,3,4,5,6,7,8,9]):
		print head

	output:
	[]
	[1]
	[1, 2]
	[1, 2, 3]
	[1, 2, 3, 4]
	[1, 2, 3, 4, 5]
	[1, 2, 3, 4, 5, 6]
	[1, 2, 3, 4, 5, 6, 7]
	[1, 2, 3, 4, 5, 6, 7, 8]
	[1, 2, 3, 4, 5, 6, 7, 8, 9]


tails(iterable) -> iterator over all tails
	example:
	for tail in tails([1,2,3,4,5,6,7,8,9]):
		print tail

	output:
	[1, 2, 3, 4, 5, 6, 7, 8, 9]
	[2, 3, 4, 5, 6, 7, 8, 9]
	[3, 4, 5, 6, 7, 8, 9]
	[4, 5, 6, 7, 8, 9]
	[5, 6, 7, 8, 9]
	[6, 7, 8, 9]
	[7, 8, 9]
	[8, 9]
	[9]
	[]


fcain(funct,*functs) -> function(...,***)
	fcain(f1,f2,...,fn)(*args,*kwargs) equals f1(f2(...fn(*args,*kwargs)))



More information about the Python-list mailing list