Python Tutorial

Showing posts with label Itertools. Show all posts
Showing posts with label Itertools. Show all posts

Saturday, May 4, 2013

Itertools - Combinatoric generators

  • product: Equivalent to nested for-loops in a generator expression.
  • permutations: Return successive r length permutations of elements in the iterable.
  • combinations: Return r length subsequences of elements from the input iterable.
  • combinations_with_replacement: Return r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.
import itertools

A = [ 1, 2, 3]
B = [4]
C = [5, 6]

print "product example:"
s=[ A, B, C ]
print list(itertools.product(*s))


print "permutations example:"
i = 1
for n in itertools.permutations(A,3):
  print i,":",n
  i += 1


print "combinations example:"  
i = 1
for n in itertools.combinations("ABCD",3):
  print i,"[:]",n
  i += 1

print "combinations_with_replacement example:"    
print list( itertools.combinations_with_replacement("ABD",3))


Output:
product example:
[(1, 4, 5), (1, 4, 6), (2, 4, 5), (2, 4, 6), (3, 4, 5), (3, 4, 6)]
permutations example:
1 : (1, 2, 3)
2 : (1, 3, 2)
3 : (2, 1, 3)
4 : (2, 3, 1)
5 : (3, 1, 2)
6 : (3, 2, 1)
combinations example:
1 [:] ('A', 'B', 'C')
2 [:] ('A', 'B', 'D')
3 [:] ('A', 'C', 'D')
4 [:] ('B', 'C', 'D')
combinations_with_replacement example:
[('A', 'A', 'A'), ('A', 'A', 'B'), ('A', 'A', 'D'), ('A', 'B', 'B'), ('A', 'B', 'D'), ('A', 'D', 'D'), ('B', 'B', 'B'), ('B', 'B', 'D'), ('B', 'D', 'D'), ('D', 'D', 'D')]

Itertools - Python iterators terminating on the shortest input sequence.

Iterators terminating on the shortest input sequence:
  • imap : Make an iterator that computes the function using arguments from each of the iterables. Always consider shortest iterables.
  • chain : Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable ans so on.
  • ifilter : Make an iterator that filters elements from iterable returning only those for which the predicate is True.
  • ifilterfalse : Make an iterator that filters elements from iterable returning only those for which the predicate is False.
  • compress : Make an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to True.
  • izip : Make an iterator that aggregates elements from each of the iterables.
  • izip_longest : Same as izip, except for uneven length values are filled by default None.
import itertools

def add(*args):
  s = 0;
  for num in args:
    s += num
  return s

print "imap example:"
# will calculate 5+2+10, 2+3+10
for n in itertools.imap(add, (5,2,3), (2, 3, 3),(10, 10)):
    print n


print "chain example"
for ch in itertools.chain('ABC','DEF'):
  print ch


print "ifilter example "
A = [1,2,4,5,7,8]
for n in itertools.ifilter(lambda x:x%2 ,A):
  print n


print "ifilterfalse example:"
for n in itertools.ifilterfalse(lambda x:x%2 ,A):
  print n


print "compress example:"
for ch in itertools.compress('abcdef',[0,1,0,1,1,1]):
  print ch

Output:
imap example:
17
15
chain example
A
B
C
D
E
F
ifilter example 
1
5
7
ifilterfalse example:
2
4
8
compress example:
b
d
e
f
izip example:
('a', 'X')
('b', 'Y')
izip_longest example:
('a', 'X')
('b', 'Y')
('c', None)
('d', None)