7 examples of 'python combinations without itertools' in Python

Every line of 'python combinations without itertools' 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
1250def combinations(c, r):
1251 # combinations('ABCD', 2) --> AB AC AD BC BD CD
1252 # combinations(range(4), 3) --> 012 013 023 123
1253 pool = tuple(range(c))
1254 n = len(pool)
1255 if r > n:
1256 return
1257 indices = list(range(r))
1258 yield tuple(pool[i] for i in indices)
1259 while True:
1260 for i in reversed(list(range(r))):
1261 if indices[i] != i + n - r:
1262 break
1263 else:
1264 return
1265 indices[i] += 1
1266 for j in range(i+1, r):
1267 indices[j] = indices[j-1] + 1
1268 yield tuple(pool[i] for i in indices)
28def testCombinations(self):
29 for i in range(15):
30 case = range(i)
31 k = random.randrange(len(case) + 1)
32 result = combinations(case, k)
33 result = set(map(tuple, result))
34 expect = itertools.combinations(case, k)
35 expect = set(expect)
36 self.assertEqual(result, expect)
51def combinations(a, b):
52 """ Returns all combinations of
53
54 - fast.forwards->fast.backwards
55 - fast.forwards->slow.backwards
56 - slow.forwards->fast.backwards
57 - slow.forwards->slow.backwards
58
59 """
60 return list(itertools.chain.from_iterable(
61 [
62 list(itertools.product(*zip(*item)))
63 for item in correspondences(a, b)
64 ]
65 ))
20def all_combinations(items):
21 variants = []
22 for n in range(len(items)+1):
23 variants.extend(itertools.combinations(items, n))
24 return variants
98async def combinations(itr: AnyIterable[T], r: int) -> AsyncIterator[Tuple[T, ...]]:
99 """
100 Yield r length subsequences from the given iterable.
101
102 Simple wrapper around itertools.combinations for asyncio.
103 This will consume the entire iterable before yielding values.
104
105 Example:
106
107 async for value in combinations(range(4), 3):
108 ... # (0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)
109
110 """
111 pool: List[T] = await list(itr)
112 for value in itertools.combinations(pool, r):
113 yield value
180def xuniqueCombinations(items, n):
181 if n == 0:
182 yield []
183 else:
184 for i in xrange(len(items)):
185 for cc in xuniqueCombinations(items[i + 1:], n - 1):
186 yield [items[i]] + cc
26def xuniqueCombinations(items, n):
27 if n==0: yield []
28 else:
29 for i in xrange(len(items)):
30 for cc in xuniqueCombinations(items[i+1:],n-1):
31 yield [items[i]]+cc

Related snippets