3 examples of 'python permute list' in Python

Every line of 'python permute list' 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
54def permute(l):
55 """
56 Return all possible permutations of l.
57
58 @type l: sequence
59 @rtype: a list of sequences
60 """
61 if len(l) == 1:
62 return [l,]
63
64 res = []
65 for i in range(len(l)):
66 l2 = list(l[:])
67 x = l2.pop(i)
68 for l3 in permute(l2):
69 l3.append(x)
70 res.append(l3)
71
72 return res
52@add_module_test(torch.float32, torch.device('cuda'), [(1, 3, 4, 5, 6)])
53def test_permute_list():
54 return Permute([0, 4, 1, 3, 2])
6def permute(a):
7 """
8 Creates all unique combinations of a list a that is passed in.
9 Function is based off of a function written by John Lettman:
10 TCHS Computer Information Systems. My thanks to him.
11 """
12
13 a.sort() # Sort.
14
15 ## Output the first input sorted.
16 yield list(a)
17
18 i = 0
19 first = 0
20 alen = len(a)
21
22 ## "alen" could also be used for the reference to the last element.
23
24 while(True):
25 i = alen - 1
26
27 while(True):
28 i -= 1 # i--
29
30 if(a[i] < a[(i + 1)]):
31 j = alen - 1
32
33 while(not (a[i] < a[j])): j -= 1 # j--
34
35 a[i], a[j] = a[j], a[i] # swap(a[j], a[i])
36 t = a[(i + 1):alen]
37 t.reverse()
38 a[(i + 1):alen] = t
39
40 # Output current.
41 yield list(a)
42
43 break # next.
44
45 if(i == first):
46 a.reverse()
47
48 # yield list(a)
49 return

Related snippets