Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PythonWordList.txt生成器_Python_Python 3.x - Fatal编程技术网

PythonWordList.txt生成器

PythonWordList.txt生成器,python,python-3.x,Python,Python 3.x,好吧,这是错误的,我能感觉到,但我似乎不知道如何正确地遍历我的第二个列表。。。。(它以正确的方式创建文件并进行迭代,但我希望使脚本更加紧凑,因为我希望能够以某种方式获得num=[0-999]。请注意,我确实意识到完成后该文件会有多大。) 我想做点什么 adj = ["united", "dramatic", "green"] noun = ["ladder", "axe", "tiger

好吧,这是错误的,我能感觉到,但我似乎不知道如何正确地遍历我的第二个列表。。。。(它以正确的方式创建文件并进行迭代,但我希望使脚本更加紧凑,因为我希望能够以某种方式获得num=[0-999]。请注意,我确实意识到完成后该文件会有多大。)

我想做点什么

adj = ["united", "dramatic", "green"]
noun = ["ladder", "axe", "tiger"]
num =["0", "1", "2"]

 
for x in range(len(adj)): 
    print(adj[x]+noun[0])
    print(adj[x]+noun[1])
    print(adj[x]+noun[2])
    
    print(adj[x]+noun[0]+num[0])
    print(adj[x]+noun[0]+num[1])
    print(adj[x]+noun[0]+num[2])

    print(adj[x]+noun[1]+num[0])
    print(adj[x]+noun[1]+num[1])
    print(adj[x]+noun[1]+num[2])

    print(adj[x]+noun[2]+num[0])
    print(adj[x]+noun[2]+num[1])
    print(adj[x]+noun[2]+num[2])
我知道这只是印刷品,但概念相同没有


非常感谢您的帮助。

要使脚本更加紧凑,我建议您查看:


使用
itertools.product

adj = ["united", "dramatic", "green"]
noun = ["ladder", "axe", "tiger"]
num = ["0", "1", "2"]

print(list(itertools.product(adj, noun, num)))

我做了一个非常简单的代码。请运行我的代码,让我知道你的意见

import itertools

filename = "mywordlist.txt"


adj = ["united", "dramatic", "green"]
noun = ["ladder", "axe", "tiger"]
num = [str(i) for i in range(4)] # from '0' to '3'

total_list = [adj, noun, num]

with open(filename, "w") as fp:
    for i in itertools.product(*total_list):
        combined_word = " ".join(i)
        print(combined_word)
        fp.write(combined_word)
        fp.write('\n')
如果只想添加adj和名词的组合,可以使用以下代码

import itertools

filename = "mywordlist.txt"

adj = ["united", "dramatic", "green"]
noun = ["ladder", "axe", "tiger"]
num = [str(i) for i in range(4)] # from '0' to '3'
num.insert(0, "")

total_list = [adj, noun, num]

with open(filename, "w") as fp:
    for i in itertools.product(*total_list):
        combined_word = " ".join(i)
        print(combined_word)
        fp.write(combined_word)
        fp.write('\n')
结果是:


完美!谢谢你们两位!现在,我要努力输入值和更多的列表。仅供参考,我注意到有时候默认的网络密码通常是形容词+名词+(三位数),我可以用零填充数字,这样我就得到了“000”-“099”???我想也许我会把它列成自己的清单,然后从100@C0D3X您可以执行
num=['{:>03d}'。为范围(3)中的i设置格式(i)]
这样您就可以得到3位数字,从左侧填充零。@Adrej Kesely超级棒!这将很容易成为一个CLI工具。如果我理解正确的话,{:>03d}。格式意味着它的格式是小数点后3位??几乎!我需要将值串联起来。看来,您在这里所做的工作可以帮助我完成上面编辑的脚本。
adj = ["united", "dramatic", "green"]
noun = ["ladder", "axe", "tiger"]
num = ["0", "1", "2"]

print(list(itertools.product(adj, noun, num)))
import itertools

filename = "mywordlist.txt"


adj = ["united", "dramatic", "green"]
noun = ["ladder", "axe", "tiger"]
num = [str(i) for i in range(4)] # from '0' to '3'

total_list = [adj, noun, num]

with open(filename, "w") as fp:
    for i in itertools.product(*total_list):
        combined_word = " ".join(i)
        print(combined_word)
        fp.write(combined_word)
        fp.write('\n')
import itertools

filename = "mywordlist.txt"

adj = ["united", "dramatic", "green"]
noun = ["ladder", "axe", "tiger"]
num = [str(i) for i in range(4)] # from '0' to '3'
num.insert(0, "")

total_list = [adj, noun, num]

with open(filename, "w") as fp:
    for i in itertools.product(*total_list):
        combined_word = " ".join(i)
        print(combined_word)
        fp.write(combined_word)
        fp.write('\n')