python - Python中将字符串拆分为n个子字符串的排列
问题描述
我有每个字母的字符串,我想将其拆分为所有可能的组合,但具有给定数量的子字符串,因此:
a = 'abcdefghi...'
分成4个子串:
comb = [['abcdefghi...'], [''], [''], ['']],[['bcdefghi...'], ['a'], [''], ['']],[['cdefghi...'], ['ab'], [''], ['']]....[[''], [''], [''], ['abcdefghi...']]
我最大的问题 rn 是对整个字母表执行此操作的时间消耗。
有任何想法吗?
解决方案
def per_string(s,n, out_list=[], lst=[]):
if s == "":
out_list.append(lst[::-1])
for i in range(len(s)):
if len(lst)<n:
lst.append([s[i:]])
per_string(s[:i], n,out_list, lst)
lst.pop()
return out_list
推荐阅读
- go - 将 GOPROXY 与 JFrog Artifactory 一起使用时找不到 404
- database - 将数据从不同的 oracle 复制到 Amazon DynamoDB
- css - Vue 组件样式永远不会消失
- jquery - 在 AJAX 和 WordPress 中找不到数据时显示消息
- jquery - 如何获得
- 剑道 TimePicker 的元素?
- php - 无法访问 AWS EC2 实例中的 Laravel 公有磁盘
- laravel - Laravel 中的护照是什么?
- c# - Android Unity - 在后台线程上加载文件
- asp.net-core - AspNetCore.SignalR 发行说明在哪里?
- javascript - 缩放图像以在浏览器中使用画布