Why are there many duplicate elements in python itertools lookups?

I am trying to find various permutations of the string "0000111". All different lines that contain three 1 and 4 0. This is my code:

p = itertools.permutations("0000111")
l = list(p)
print len(l) #5040
print len(set(l)) #35

What happened? And is there a better way?

+4
source share
1 answer

In the manual: http://docs.python.org/2.7/library/itertools.html#itertools.permutations

Elements are considered unique, based on their position, and not on their value. Therefore, if the input elements are unique, there will be no duplicate values ​​in each permutation.

This means that it itertools.permutations('000')will happily give you 3! = 6, where everything is '000'.

: http://ideone.com/gzqolT


, . itertools.combinations(S,r) S r. S = range(7) r = 3.

:

list('{0:07b}'.format(sum(subset))
     for subset in itertools.combinations((2**s for s in range(7)), 3))

:

  • (2**s for s in range(7)) 2 2 ^ 6.
  • itertools.combinations(…, 3) 3 2.
  • sum(subset) , . [1,2,4]= 7.
  • '{0:07b}'.format(…) int , 7.
+8

All Articles