這是我的第一個問題,所以請原諒我的格式。我基本上是在嘗試使用大小為 k 的重復來創建排列,并且沒有彼此相鄰的重復元素。所以,我有一個像 1,2,3,4 和 ak 值的串列。例如,通過取 1,2,3,4 和 k=4,我們將得到一個包含大小為 k 且沒有兩個相同連續項的所有可能排列的串列,例如:1,2,3,4 1, 2,3,2 1,2,3,1 ..等等,我可以遞回地進行基本的排列,但我已經為此苦苦掙扎了一段時間。我不想匯入任何庫,因為我認為沒有它也可以完成,我想將所有組合附加到串列中。任何幫助,將不勝感激!
uj5u.com熱心網友回復:
要生成所有排列,您可以向下傳遞當前的部分排列并依次添加每個專案,然后再呼叫遞回添加另一個位置(跳過重復的專案):
def perm(A,k,p=[]):
if not k: # size reached
yield p
return
for n in A: # combine every item
if p and n == p[-1]: continue # except the repeated one
yield from perm(A,k-1,p [n])
輸出:
# place in a list:
permList = list(perm([1,2,3,4],4))
# or access sequentially without storing:
for i,p in enumerate(perm([1,2,3,4],4)): print(i,p)
0 [1, 2, 1, 2]
1 [1, 2, 1, 3]
2 [1, 2, 1, 4]
3 [1, 2, 3, 1]
4 [1, 2, 3, 2]
5 [1, 2, 3, 4]
6 [1, 2, 4, 1]
7 [1, 2, 4, 2]
8 [1, 2, 4, 3]
9 [1, 3, 1, 2]
10 [1, 3, 1, 3]
11 [1, 3, 1, 4]
12 [1, 3, 2, 1]
13 [1, 3, 2, 3]
14 [1, 3, 2, 4]
15 [1, 3, 4, 1]
16 [1, 3, 4, 2]
17 [1, 3, 4, 3]
18 [1, 4, 1, 2]
19 [1, 4, 1, 3]
20 [1, 4, 1, 4]
21 [1, 4, 2, 1]
22 [1, 4, 2, 3]
23 [1, 4, 2, 4]
24 [1, 4, 3, 1]
25 [1, 4, 3, 2]
26 [1, 4, 3, 4]
27 [2, 1, 2, 1]
28 [2, 1, 2, 3]
29 [2, 1, 2, 4]
30 [2, 1, 3, 1]
31 [2, 1, 3, 2]
32 [2, 1, 3, 4]
33 [2, 1, 4, 1]
34 [2, 1, 4,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103 [4, 3, 2, 3]
104 [4, 3, 2, 4]
105 [4, 3, 4, 1]
106 [4, 3, 4, 2]
107 [4, 3, 4, 3]
該函式是一個生成器,它允許您按順序獲取排列,而不必將它們全部存盤在串列中(串列很容易變得太大并且需要很長時間來生成)。
如果您想像擁有串列一樣操作排列但不生成實際串列,則可以創建一個函式,該函式將為您提供“虛擬”串列中的第 N 個排列:
def permCount(A,k): return len(A)*(len(A)-1)**(k-1)
def permAtIndex(A,k,i):
p = [len(A)] # positions of elements
c = (len(A)-1)**(k-1) # chunk of sub-permutations
for _ in range(k): # for required size
j,i = divmod(i,c) # j is index at position
c //= len(A)-1 # next chunk size
p.append(j (j>=p[-1])) # adjust index for non-repeat
return [A[j] for j in p[1:]] # build permutation from indexes
這將為您提供排列“串列”的大小,并允許您在給定索引處獲得排列(如果需要,您可以隨機選擇):
permCount([1,2,3,4],4) # 108
permAtIndex([1,2,3,4],4,84) # [4, 1, 3, 1]
permCount(range(15),10) # 309915701760
permAtIndex(range(15),10,123456789101) # [5, 14, 9, 2, 5, 10, 7, 3, 0, 10]
[編輯]直接回傳一個串列:
def perm(A,k,p=[]):
if not k: # size reached
return [p]
result = []
for n in A: # combine every item
if p and n == p[-1]: continue # except the repeated one
result = perm(A,k-1,p [n])
return result
uj5u.com熱心網友回復:
只回傳一個排列:
我知道,您不想使用庫,但您需要具有某種隨機性:
from random import sample
k = 4
l = [i for i in range(k)]
sample(l, k)
這將是遞回的:
from random import randint
def recursvie_sample(l, k):
if k == 1:
return l
else:
return [l.pop(randint(0, k - 1))] recursvie_sample(l, k - 1)
k = 4
l = [i for i in range(k)]
recursvie_sample(l.copy(), k)
注意:復制串列l作為通過函式呼叫recursive_sample保持l不變的輸入值,之后您仍然可以使用它......
編輯
列出沒有任何數字出現多次的所有排列
def perm(l1, l2 = []):
if not l1:
result.append(l2)
else:
for i in range(len(l1)):
l1_copy = l1.copy()
l2_copy = l2.copy()
l2_copy.append(l1_copy.pop(i))
perm(l1_copy, l2_copy)
result = []
initList = [i for i in range(4)]
perm(initList)
print(result)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/388365.html
上一篇:找到通過無向無環圖的所有路徑
