推 LP9527: tuple+ set幾行就搞定了 01/22 13:28
→ LP9527: 喔看錯了 01/22 13:47
推 sherees: itertools.combinations 01/22 14:55
→ ro9956882: [['a','b'],['c']]我用['ab','c']表示 這樣過程中不用 01/23 00:15
→ ro9956882: 用到deepcopy 要轉換格式做完再一次轉 01/23 00:15
→ ro9956882: 如果有重複元素(多個'a')就要再改一下 0.0 01/23 00:21
elements = list("abcdefghijl")
combinations = [[]]
for element in elements :
for i in range(len(combinations)):
for j in range(len(combinations[i])):
new = combinations[i][:]
new[j] += element
combinations.append(new)
combinations[i].append(element)
combinations = [list(sorted([list(c) for c in combination]))
for combination in combinations]
combinations = list(sorted(combinations))
我重抄一份在此
※ 編輯: sharkbay (106.1.116.121 臺灣), 01/23/2021 00:50:44
def partition(collection):
global counter
if len(collection) == 1:
yield [collection]
return
first = collection[0]
for smaller in partition(collection[1:]):
for n, subset in enumerate(smaller):
yield smaller[:n] + [[first] + subset] + smaller[n + 1:]
yield [[first]] + smaller
google到一份新的code
※ 編輯: sharkbay (106.1.116.121 臺灣), 01/23/2021 06:20:52