可能なすべての文字の組み合わせを含む単語のリストを作成しようとしていますが、それぞれのリストで複数の文字を変更しています。入力はキーワードのリストでもあります。例:所望の結果は、このようなリストになりpython - リストとの組み合わせの置換
keywords=["magic", "mate"]
aoptions = ["a", "4", "@"]
boptions = ["b", "8"]
eoptions = ["e", "3"]
goptions = ["g", "9"]
ioptions = ["i", "1"]
loptions = ["l", "1"]
ooptions = ["o", "0"]
soptions = ["s", "5", "$"]
toptions = ["t", "7"]
zoptions = ["z", "2"]
:私は一度に1つのキーワードのためのソリューションを作成することができました
['magic', 'mag1c', 'ma9ic', 'ma91c'...'[email protected]', '[email protected]']
と別の単一で単一の文字に置き換えてキャラクター。アルゴリズムはここに見つかりましたString Replacement Combinations。これは特に重要ではありませんが、キーワードのリストは、各ライン上の単一のキーワードで.txtファイルから読み込まれます
>>> filler("magic", "a", "4")
<generator object <genexpr> at 0x8fa798c>
>>> list(filler("magic", "a", "4"))
['magic', 'm4gic']
と:それは、その結果、この
from itertools import product
def filler(word, from_char, to_char):
options = [(c,) if c != from_char else (from_char, to_char) for c in word]
return (''.join(o) for o in product(*options))
のように見えます結果の組み合わせリストは、各行に1つの単語を含む.txtファイルに書き込まれます。私は別の反復ループを作成し、itertools.productの例を何も変わらずに修正しようとしていました。すべての助けは大いに感謝しています。
UPDATE:#zefciuのアドバイスに基づいて私のフィラー機能の更新 私はこの方法を使用して解決することができました:すべての
from itertools import product
def filler(word):
combos = [(c,) if c not in options else options[c] for c in word]
return (''.join(o) for o in product(*combos))
options = {
'A': ['A', '4', '@'],
'B': ['B', '8',],
'E': ["E", "3"],
'G': ["G", "9"],
'I': ["I", "1", "!"],
'L': ["L", "1"],
'O': ["O", "0"],
'S': ["S", "5", "$"],
'T': ["T", "7"],
'Z': ["Z", "2"]}
with open('CustomList.txt', 'r') as f:
startlist = f.readlines()
startlist = [x.strip() for x in startlist]
startlist = [element.upper() for element in startlist]
filid= open('WordList.txt', 'w+')
for word in startlist:
temp_list=list(filler(word))
for newword in temp_list:
print >> filid, newword
filid.close()
ありがとうございました。これを見てから数分で問題を解決できました。私は私の現在のソリューションで投稿を編集しました。 – jca12