使用单词列表进行Python字符串比较

jes*_*Kat 1 python list string-comparison

最终我将能够在聊天室中发布这样的简单问题,但是现在我必须发布它.我仍在努力解决Python中的比较问题.我有一个包含我从文件中获取的字符串的列表.我有一个函数,它接受单词列表(以前从文件创建)和一些'密文'.我试图使用Shift Cipher破解密文.我的问题与比较整数是一样的.虽然我可以看到在尝试使用print语句进行调试时,我的密文将被转移到单词列表中的单词,但它永远不会计算为True.我可能正在比较两种不同的变量类型,或者/ n可能会抛弃比较.对于今天的所有帖子感到抱歉,我今天正在做很多练习问题,为即将到来的作业做准备.

  def shift_encrypt(s, m):

   shiftAmt = s % 26
   msgAsNumList = string2nlist(m)

   shiftedNumList = add_val_mod26(msgAsNumList, shiftAmt)
   print 'Here is the shifted number list: ', shiftedNumList

   # Take the shifted number list and convert it back to a string
   numListtoMsg = nlist2string(shiftedNumList)
   msgString = ''.join(numListtoMsg)

   return msgString

 def add_val_mod26(nlist, value):
   newValue = value % 26
   print 'Value to Add after mod 26: ', newValue
   listLen = len(nlist)
   index = 0
   while index < listLen:
       nlist[index] = (nlist[index] + newValue) % 26
       index = index + 1
   return nlist

 def string2nlist(m):
   characters =    ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
   numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
   newList = []
   msgLen = len(m)         # var msgLen will be an integer of the length

   index = 0               # iterate through message length in while loop
   while index < msgLen:
       letter = m[index]   # iterate through message m
       i = 0
       while i < 26:
           if letter == characters[i]:
               newList.append(numbers[i])
           i = i + 1
       index = index + 1
    return newList

  def nlist2string(nlist):
    characters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    numbers = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
    newList = []
    nListLen = len(nlist)

    index = 0
    while index < nListLen:
        num = nlist[index]
        newNum = num % 26
        i = 0
        while i < 26:
            num1 = newNum
            num2 = numbers[i]
            if (num1 == num2):
            newList.append(characters[i])
        i = i + 1
    index = index + 1
    return newList


  def wordList(filename):

    fileObject = open(filename, "r+")                    
    wordsList = fileObject.readlines()
    return wordsList


  def shift_computePlaintext(wlist, c):

    index = 0
    while index < 26:
        newCipher = shift_encrypt(index, c)
        print 'The new cipher text is: ', newCipher
        wordlistLen = len(wlist)
        i = 0
        while i < wordlistLen:
            print wlist[i]
            if newCipher == wlist[i]:
                return newCipher
            else:
                print 'Word not found.'
            i = i + 1
        index = index + 1    

   print 'Take Ciphertext and Find Plaintext from Wordlist Function: \n'
   list = wordList('test.txt')
   print list
   plainText = shift_computePlaintext(list, 'vium')
   print 'The plaintext was found in the wordlist: ', plainText
Run Code Online (Sandbox Code Playgroud)

当移位量= 18时,密文=名称,这是我的单词列表中的单词,但它永远不会计算为True.在此先感谢您的帮助!!

aba*_*ert 5

目前为止我们很难确定我们掌握的信息,但这是一个猜测:

wordsList = fileObject.readlines()
Run Code Online (Sandbox Code Playgroud)

这将返回一个list保留换行符的字符串,如:

['hello\n', 'my\n', 'name\n', 'is\n', 'jesi\n']
Run Code Online (Sandbox Code Playgroud)

所以,在内部shift_computePlaintext,当你迭代wlist寻找与解密相匹配的东西时'vium',你正在寻找一个匹配的字符串'name',并且它们都不匹配,包括'name\n'.

换句话说,正是你所怀疑的.

有几种方法来解决这个问题,但最明显的是使用wlist[i].strip()替代wlist[i],或者使用类似剥离摆在首位的一切wordsList = [line.strip() for line in fileObject],而不是wordsList = fileObject.readlines().


一些附注:

几乎没有理由打电话readlines().这将返回一个可以迭代的行列表......但是文件对象本身已经是可以迭代的可迭代行.如果你真的需要确保它是一个列表而不是其他类型的可迭代,或者为以后制作单独的副本,或者其他什么,只需调用list它,就像使用任何其他可迭代一样.

你几乎不应该写这样的循环:

index = 0
while index < 26:
    # ...
    index = index + 1
Run Code Online (Sandbox Code Playgroud)

相反,只需这样做:

for index in range(26):
Run Code Online (Sandbox Code Playgroud)

它更容易阅读,很难得到错误的(微妙的off-by-一个错误负责一半的令人沮丧的调试,你会在你的一生做的),等等.

如果你在一个集合的长度上循环,甚至不要这样做.而不是这个:

wordlistLen = len(wlist)
i = 0
while i < wordlistLen:
    # ...
    word = wlist[i]
    # ...
    i = i + 1
Run Code Online (Sandbox Code Playgroud)

......这样做:

for word in wlist:
Run Code Online (Sandbox Code Playgroud)

......或者,如果你需要两者iword(你偶尔会这样做):

for i, word in enumerate(wlist):
Run Code Online (Sandbox Code Playgroud)

同时,如果你循环收集一个集合的唯一原因是检查它的每个值,你甚至不需要它.而不是这个:

wordlistLen = len(wlist)
while i < wordlistLen:
    print wlist[i]
    if newCipher == wlist[i]:
        return newCipher
    else:
        print 'Word not found.'
    i = i + 1
Run Code Online (Sandbox Code Playgroud)

......这样做:

if newCipher in wlist:
    return newCipher
else:
    print 'Word not found.'
Run Code Online (Sandbox Code Playgroud)

在这里,你实际上已经得到了那些微妙的错误之一:您打印"一词没有发现"了个遍,而不是如果没有找到,只在最后打印一次.