5 examples of 'python permutations with repetition' in Python

Every line of 'python permutations with repetition' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
10def testPermutations(self):
11 for i in range(7):
12 case = list(range(i))
13 result = permutations(case)
14 result = set(map(tuple, result))
15 expect = itertools.permutations(case)
16 expect = set(expect)
17 self.assertEqual(result, expect)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

24@cython.locals(n=int, i=int, j=int)
25def permutations(iterable):
26 """permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)"""
27 pool = tuple(iterable)
28 n = len(pool)
29 indices = list(range(n))
30 cycles = list(range(1, n+1))[::-1]
31 yield [ pool[i] for i in indices ]
32 while n:
33 for i in reversed(range(n)):
34 j = cycles[i] - 1
35 if j == 0:
36 indices[i:] = indices[i+1:] + indices[i:i+1]
37 cycles[i] = n - i
38 else:
39 cycles[i] = j
40 indices[i], indices[-j] = indices[-j], indices[i]
41 yield [ pool[i] for i in indices ]
42 break
43 else:
44 return
18def permutations(nums):
19 if len(nums) <= 1:
20 return [nums]
21
22 last = nums[0]
23 oldlist = permutations(nums[1:])
24 newlist = []
25
26 for alist in oldlist:
27 for index in range(len(alist)+1):
28 nlist = list(alist)
29 nlist.insert(index, last)
30 newlist.append(nlist)
31 return newlist
138def _find_repeats(permutations):
139 """
140 Returns a set of repetitive blocks
141 """
142 index = defaultdict(set)
143 repeats = set()
144 for perm in permutations:
145 for block in perm.blocks:
146 if perm.genome_name in index[block.block_id]:
147 repeats.add(block.block_id)
148 else:
149 index[block.block_id].add(perm.genome_name)
150 return repeats
10def perm(iterable):
11 return list(itertools.permutations(iterable))

Related snippets