9 examples of 'python combinations of two lists' in Python

Every line of 'python combinations of two lists' 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
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 ))
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

20def all_combinations(items):
21 variants = []
22 for n in range(len(items)+1):
23 variants.extend(itertools.combinations(items, n))
24 return variants
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)
15def combine1(self, n, k):
16 """
17 :param n:
18 :param k:
19 :return:
20
21 Recursive
22 """
23 if k == 0:
24 return [[]]
25 return [pre + [i] for i in range(1, n + 1) for pre in self.combine(i - 1, k - 1)]
213def get_combinations(to_combine):
214 combination_found = True
215 while combination_found:
216 combination_found = False
217 combos = itertools.combinations(to_combine,2)
218 removed = []
219 for d in combos:
220
221 if sum([(d[0] == r) or (d[1] == r) for r in removed]):
222 continue
223
224 if d[0].issuperset(d[1]):
225 [to_combine.remove(x) for x in to_combine if x == d[1]]
226 removed.append(d[1])
227 elif d[1].issuperset(d[0]):
228 [to_combine.remove(x) for x in to_combine if x == d[0]]
229 removed.append(d[0])
230 elif len(d[0].intersection(d[1])) > 0:
231 combination_found = True
232 to_combine.append(set.union(d[0],d[1]))
233 [to_combine.remove(x) for x in to_combine if x == d[0]]
234 [to_combine.remove(x) for x in to_combine if x == d[1]]
235 removed.append(d[0])
236 removed.append(d[1])
237 return to_combine
13def get_combinations(yag):
14 """ Creates permutations of possible inputs """
15 tos = (None, (yag.user), [yag.user, yag.user],
16 {yag.user: 'me', yag.user + '1': 'me'})
17 subjects = ('subj', ['subj'], ['subj', 'subj1'])
18 contents = (None, ['body'], ['body', 'body1', '<h2><center>Text</center></h2>'])
19 results = []
20 for row in itertools.product(tos, subjects, contents):
21 options = {y: z for y, z in zip(['to', 'subject', 'contents'], row)}
22 options['preview_only'] = True
23 results.append(options)
24
25 return results
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