简体   繁体   English

生成已知字符的单词表

[英]Generate wordlist with known characters

I'm looking to write a piece of code in Javascript or Python that generates a wordlist file out of a pre-defined combination of characters.我正在寻找在 Javascript 或 Python 中编写一段代码,该代码从预定义的字符组合中生成一个词表文件。

Eg input = abc output = ABC abc Abc aBc abC AbC ABc aBC例如输入 = abc output = ABC abc Abc aBc abC AbC ABc aBC

I have very basic knowledge of either so all help is appreciated.我对这两者都有非常基本的了解,因此感谢所有帮助。

Thank you谢谢

I'll assume that you're able to import Python packages.我假设您能够导入 Python 包。 Therefore, take a look at itertools.product :因此,看一下itertools.product

This tool computes the cartesian product of input iterables.该工具计算输入迭代的笛卡尔积。

For example, product(A, B) returns the same as ((x,y) for x in A for y in B).例如,product(A, B) 返回与 ((x,y) for x in A for y in B) 相同的结果。

It looks quite like what you're looking for, right?它看起来很像您正在寻找的东西,对吧? That's every possible combination from two different lists.这是来自两个不同列表的所有可能组合。

Since you're new to Python, I'll assume you don't know what a map is.由于您是 Python 的新手,我假设您不知道map是什么。 Nothing too hard to understand:没有什么太难理解的:

Returns a list of the results after applying the given function to each item of a given iterable (list, tuple etc.)将给定的 function 应用于给定可迭代对象(列表、元组等)的每个项目后,返回结果列表

That's easy.这很容易。 So the first parameter is the function you want to apply and the second one is your iterable.因此,第一个参数是您要应用的 function,第二个参数是您的可迭代参数。

The function I applied in the map is as follows:我在map中应用的function如下:

''.join

This way you set '' as your separator (basically no separator at all) and put together every character with.join.这样,您将 '' 设置为分隔符(基本上根本没有分隔符)并将每个字符与.join 放在一起。

Why would you want to put together the characters?为什么要把角色放在一起? Well, you'll have a list (a lot of them in fact) and you want a string, so you better put those chars together in each list.好吧,您将有一个列表(实际上有很多)并且您想要一个字符串,因此您最好将这些字符放在每个列表中。

Now here comes the hard part, the iterable inside the map:现在是困难的部分,map 内部的可迭代:

itertools.product(*((char.upper(), char.lower()) for char in string)

First of all notice that * is the so-called splat operator in this situation.首先注意*在这种情况下是所谓的splat运算符。 It splits the sequence into separate arguments for the function call.它将序列拆分为单独的 arguments 用于 function 调用。

Now that you know that, let's dive into the code.现在您知道了,让我们深入研究代码。 Your (A, B) for itertools.product(A, B) are now (char.upper(), char.lower()).您的 itertools.product(A, B) 的 (A, B) 现在是 (char.upper(), char.lower())。 That's both versions of char , upper and lowercase.这是char的两个版本,大写和小写。 And what's char ?什么是char It's an auxiliar variable that will take the value of each and every character in the given string, one at a time.这是一个辅助变量,它将获取给定字符串中每个字符的值,一次一个。

Therefore for input 'abc' char will take values a, b and c while in the loop, but since you're asking for every possible combination of uppercase and lowercase char you'll get exactly what you asked for.因此,对于输入 'abc' char将在循环中采用值 a、b 和 c,但由于您要求大写和小写char的每种可能组合,您将得到您所要求的。

I hope I made everything clear enough.我希望我把一切都说清楚了。 :) :)

Let me know if you need any further clarification in the comments.如果您需要在评论中进一步澄清,请告诉我。 Here's a working function based on my previous explanation:根据我之前的解释,这是一个有效的 function:

import itertools

def func():
    string = input("Introduce some characters: ")
    output = map(''.join, itertools.product(*((char.upper(), char.lower()) for char in string)))
    print(list(output))

As an additional note, if you printed output you wouldn't get your desired output, you have to turn the map type into a list for it to be printable.作为附加说明,如果您打印output ,您将无法获得所需的 output,您必须将 map 类型转换为可打印的列表。

A simple approach using generators, and no library code.使用生成器的简单方法,没有库代码。 It returns a generator (iterator-like object), but can be converted to a list easily.它返回一个生成器(类似迭代器的对象),但可以轻松转换为列表。

def lU(s):
    if not s:
        yield ''
    else:
        for sfx in lU(s[1:]):
            yield s[0].upper() + sfx
            yield s[0].lower() + sfx

print list(lU("abc"))

Note that all the sub-lists of suffixes are not fully expanded, but the number of generator objects (each a constant size) that get generated is proportional to the length of the string.请注意,后缀的所有子列表都没有完全展开,但是生成的生成器对象的数量(每个都是恒定大小)与字符串的长度成正比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM