在这个 kata 中,您必须创建非空输入字符串的所有排列并删除重复项(如果存在)。这意味着,您必须以所有可能的顺序对输入中的所有字母进行洗牌。
例子:
* With input 'a'
* Your function should return: ['a']
* With input 'ab'
* Your function should return ['ab', 'ba']
* With input 'aabb'
* Your function should return ['aabb', 'abab', 'abba', 'baab', 'baba', 'bbaa']
排列顺序不一定;
import itertools as it
string = 'aabb'
le = len(string)
res = []
for s in it.permutations(string, le):
res.append(''.join(s))
res = list(set(res))
print(res)
本文介绍如何使用Python的itertools模块生成输入字符串的所有非空排列,并移除重复项,以展示字符串洗牌的基本操作。通过实例演示了如何处理如'aabb'这样的字符串,输出所有可能的不同排列组合。
514





