-1

我试着玩文字游戏。例如,我从 txt 文件中读取单词“script”。然后想做所有字母的突变并写下每个字母。所以这里有变化

 s=$
 s=5
 s=S
 s=s
 c=(
 c=[
 c={
 c=<
 c=c
 c=C
 r=r
 r=R
 i=i
 i=I
 i=|
 i=1
 i=!
 .
 .
 .

我想得到的是

 scrypt
 $crypt
 5cript
 Scrypt
 s(ript
 $(cript
 .
 .
 .

所以所有可能的组合。类似于 itertool.product 我有点困惑如何做到这一点。我是这样开始的

 def main():

    with open('file1.txt', 'r') as f1, open('file2.txt', 'w') as f2:
       for word in f1:
           l=len(word)
           for i in range(l):
               if word[i] = s:
                   word1=word[i].raplace("$") #don't know if sintacs is ok
                   f2.write(word1)
               else: 
                   if word[i] = c:
                .
                .

现在我在这里很困惑。我将不得不询问字母表中的每一个字母,我让这变得复杂了。

我可能需要在循环中有很多循环。我认为单词中有多少个字符就是循环的手动操作。


IT 变得复杂并且可能会变慢。有什么简单的方法吗?一些功能(工具)要导入?

问题是如何处理一个单词中的相同字母以及如何处理

PS我正在使用python 3.4.2

4

2 回答 2

1

构造一个字典,将每个字母映射到其允许的替换。然后使用itertools.product查找这些替换的所有组合。

import string
import itertools

replacements = """
s=$
s=5
s=S
c=(
c=[
c={
c=<
c=C
r=R
i=I
i=|
i=1
i=!
"""


d = {c:[c] for c in string.printable}
for line in replacements.strip().split("\n"):
    c, replacement = line.split("=")
    d[c].append(replacement)

word = "script"
for letters in itertools.product(*[d[c] for c in word]):
    print("".join(letters))

结果:

script
scrIpt
scr|pt
scr1pt
...
S(R1pt
S(R!pt
S[ript
S[rIpt
...
SCRIpt
SCR|pt
SCR1pt
SCR!pt
于 2015-09-15T14:23:52.013 回答
0

好的,我编写了代码并尝试了它。我符文的第一个是这个

 import string
 import itertools
 def main():
      with open('mutation.txt', 'r') as l1, open('replacements.txt', 'r') as l2, open('mutator.txt', 'w') as l3:
         d = {c:[c] for c in string.printable}
         for line in replacements.strip().split("\n"):
             c, replacement = line.split("=")
             d[c].append(replacement)

         for word in l1:
             for letters in itertools.product(*[d[c] for c in word]):
                 l3.write("".join(letters))
      print("done")
if __name__ == "__main__": main()

它没有奏效。问题在于更换。它无法读取或必须以其他方式写入。

我第二次尝试像@Kevin 那样替换。它只适用于大量副本(重复)。38 字节的文件为 37.4MB。删除重复项后,它下降到 1.27MB。这么多重复。有效且有重复的代码是这个

 import string
 import itertools
 def main():
      replacements = """
      a=a
      a=A
      a=@
      a=4
      b=b
      b=B
      b=6
      c=c
      c=C
      c=<
      c={
      c=[
      d=d
      d=D
      e=e
      e=E
      e=3
      f=f
      f=F
      f=%
      f=8
      g=g
      g=G
      g=9
      h=h
      h=H
      h=#
      i=i
      i=I
      i=!
      i=1
      i=|
      j=j
      j=J
      j=]
      j=>
      j=}
      j=)
      k=k
      k=K
      l=l
      l=L
      l=!
      l=1
      l=|
      m=m
      m=M
      n=n
      n=N
      o=o
      o=O
      o=0
      p=p
      p=P
      r=r
      r=R
      s=s
      s=S
      s=$
      s=5
      t=t
      t=T
      t=+
      t=7
      u=u
      u=U
      v=v
      v=V
      z=z
      z=Z
      z=2
      """
      with open('mutation.txt', 'r') as l1, open('replacements.txt', 'r') as l2, open('mutator.txt', 'w') as l3:
         d = {c:[c] for c in string.printable}
         for line in replacements.strip().split("\n"):
             c, replacement = line.split("=")
             d[c].append(replacement)

         for word in l1:
             for letters in itertools.product(*[d[c] for c in word]):
                 l3.write("".join(letters))
      print("done")
if __name__ == "__main__": main()

我注意到打开replacements.txt 后我没有使用l2。所以我把这段代码

 for line in l2.strip().split("\n"):

运行它而不用代码替换(不是@Kevin方式)它没有工作。(这是我写的这个 2 的第一个代码)。然后我使用@kevin 的方式并在代码中放入替换

所以我可以在代码中进行替换,但如何解决重复问题?

于 2015-09-15T16:50:04.783 回答