首页 > 代码库 > 获取一段字符串的所有排列组成结果(不区分次序)
获取一段字符串的所有排列组成结果(不区分次序)
#!/usr/bin/python3 # -*- coding: utf-8 -*- import sys,math import random import pprint b = "abcdedf" a = list(b) def test(a,n): if n==1: return [i for i in a] length = len(a) res = [] for i in range(length-n+1): res += [a[i] + child for child in test(a[i+1:],n-1)] return res def total(a): res = [] for i in range(1,len(a)): for j in test(a,i): res .append(j) return res print(total(a))
result:
[‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘d‘, ‘f‘, ‘ab‘, ‘ac‘, ‘ad‘, ‘ae‘, ‘ad‘, ‘af‘, ‘bc‘, ‘bd‘, ‘be‘, ‘bd‘, ‘bf‘, ‘cd‘, ‘ce‘, ‘cd‘, ‘cf‘, ‘de‘, ‘dd‘, ‘df‘, ‘ed‘, ‘ef‘, ‘df‘, ‘abc‘, ‘abd‘, ‘abe‘, ‘abd‘, ‘abf‘, ‘acd‘, ‘ace‘, ‘acd‘, ‘acf‘, ‘ade‘, ‘add‘, ‘adf‘, ‘aed‘, ‘aef‘, ‘adf‘, ‘b
cd‘, ‘bce‘, ‘bcd‘, ‘bcf‘, ‘bde‘, ‘bdd‘, ‘bdf‘, ‘bed‘, ‘bef‘, ‘bdf‘, ‘cde‘, ‘cdd‘, ‘cdf‘, ‘ced‘, ‘cef‘, ‘cdf‘, ‘ded‘, ‘def‘, ‘ddf‘, ‘edf‘, ‘abcd‘, ‘abce‘, ‘abcd‘, ‘abcf‘, ‘abde‘, ‘abdd‘, ‘abdf‘, ‘abed‘, ‘abef‘, ‘abdf‘, ‘acde‘, ‘acdd‘, ‘acdf‘, ‘aced‘, ‘acef‘, ‘acdf‘, ‘ad
ed‘, ‘adef‘, ‘addf‘, ‘aedf‘, ‘bcde‘, ‘bcdd‘, ‘bcdf‘, ‘bced‘, ‘bcef‘, ‘bcdf‘, ‘bded‘, ‘bdef‘, ‘bddf‘, ‘bedf‘, ‘cded‘, ‘cdef‘, ‘cddf‘, ‘cedf‘, ‘dedf‘, ‘abcde‘, ‘abcdd‘, ‘abcdf‘, ‘abced‘, ‘abcef‘, ‘abcdf‘, ‘abded‘, ‘abdef‘, ‘abddf‘, ‘abedf‘, ‘acded‘, ‘acdef‘, ‘acddf‘, ‘ac
edf‘, ‘adedf‘, ‘bcded‘, ‘bcdef‘, ‘bcddf‘, ‘bcedf‘, ‘bdedf‘, ‘cdedf‘, ‘abcded‘, ‘abcdef‘, ‘abcddf‘, ‘abcedf‘, ‘abdedf‘, ‘acdedf‘, ‘bcdedf‘]
获取一段字符串的所有排列组成结果(不区分次序)