我试图找到一种方法来解读单词并在单词列表中找到它们。一切正常,但每次运行脚本时,都会返回不同的输出。
即。我的争吵语是:
ctatmo hesena eysrrh nsgesei ncctone phjeso htdgwi lrqaeu gcsoguo rmuhpy
首先输出:
的Sheena,成因,拉奎尔,雪利酒,墨菲,德怀特,gocougs,Tomcat的,约瑟夫,连接
第二次输出:
的Sheena,Tomcat的,墨菲,拉奎尔,雪利酒,成因,连接,德怀特,约瑟夫,gocougs
我想按照与乱码相同的顺序打印这些单词 - ctatmo = tomcat,hesena = shena等等,我不知道如何实现。
这是我的代码:
from random import shuffle
from os import system
x = 0
wordlist = []
passlist = []
correct_passwords = []
bad = []
def add_words():
dic = open('wordlist.txt')
for line in dic.readlines():
wordlist.append(line.strip())
dic.close()
pwd = open('stuff.txt')
for line in pwd.readlines():
passlist.append(sorted(line.strip()))
pwd.close()
def shuffle_words(args):
args = list(args)
shuffle(args)
global shuffled_word
shuffled_word = ''.join(args)
def check_if_correct():
if (shuffled_word in wordlist) and (shuffled_word not in correct_passwords):
correct_passwords.append(shuffled_word)
else:
bad.append(shuffled_word)
def copy_to_clip():
stuff = '{},{},{},{},{},{},{},{},{},{}'.format(correct_passwords[0],correct_passwords[1],correct_passwords[2],correct_passwords[3],correct_passwords[4],correct_passwords[5],
correct_passwords[6],correct_passwords[7],correct_passwords[8],correct_passwords[9])
cmd = 'echo '+stuff.strip()+'|clip'
return system(cmd)
def main():
add_words()
while len(correct_passwords) < 10:
for words in passlist:
shuffle_words(words)
check_if_correct()
print(correct_passwords)
copy_to_clip()
if __name__ == '__main__':
main()
有人可以帮助我吗?我是python的新手。提前谢谢。
答案 0 :(得分:0)
您使用random.shuffle()
尝试随机排序,直到找到匹配的顺序,因此每次运行脚本时,您都会找到不同顺序的单词的正确排序。
这是问题的答案,但我要提一下,除了尝试随机订单之外,还有更好的方法可以做到这一点,直到找到匹配为止。您可能想要在Code Review上打开另一个关于改进代码的方法的问题。