所有带有字符列表的字符串? [英] All strings with list of characters?

查看:29
本文介绍了所有带有字符列表的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作类似于Ripper的专用实用程序,并且我想使用一个循环,该循环返回所有字符串,最多可以由该字符串形成x个字符.例如,如果种子"字符串为 abcd ,则应返回:

I'm making a specialized utility similar to John the Ripper, and I'd like to use a loop that returns all strings up to x characters that can be formed from the string. For example, if the "seed" string is abcd, it should return:

a
b
c
d
aa
ab
ac

,依此类推.如果字符限制为10,则会生成 aaaaaaaaaa abcddcbaaa 等.是否有一个简单的 for 循环来执行此操作,还是比这更复杂?

and so on. If the character limit is 10, it would generate aaaaaaaaaa, abcddcbaaa, and so on. Is there a simple for loop to do this, or is it more complicated than that?

推荐答案

我将从

I'll self-plagiarize from this answer and add a maximum length:

from itertools import product

def multiletters(seq, max_length):
    for n in range(1, max_length+1):
        for s in product(seq, repeat=n):
            yield ''.join(s)

给出

>>> list(multiletters("abc", 2))
['a', 'b', 'c', 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
>>> list(multiletters("abcd", 4))[:8]
['a', 'b', 'c', 'd', 'aa', 'ab', 'ac', 'ad']

以此类推.

这篇关于所有带有字符列表的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆