我試圖比較一下,看看列表中的單詞是字典全部還是單詞。我正在編寫解碼txt文件的程序。解碼比較列表與字典
這裏說的是一個行:
['Now', 'we', 'are', 'engaged', 'in', 'a', 'great', 'civil', 'war,']
我想去的字典和檢查,看看是否有這些詞都在那裏。如果是這樣,我會把這些字符串放在一起並寫入一個文件。我想知道的是如何比較兩者。由於所有單詞都是小寫,因此我首先將小寫的第一個單詞小寫。
我的字典裏的一個例子是:
{"now": "", "help": "", "you": ""}
,但充滿了更多的話。
如果你想看到我的整個代碼只是問:)
這是我製作的字典代碼。每一行都是一個字。
f = open('dictionary.txt', "r")
dictionary = {}
for line in f:
word = line.strip()
dictionary[word] = ""
print dictionary
更新
def CaeserCipher(string, k):
#setting up variables to move through
upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'*10000
lower = 'abcdefghijklmnopqrstuvwxyz'*10000
newCipher = ''
#looping each letter and moving it k times
for letter in string:
if letter in upper:
if upper.index(letter) + k > 25:
indexPosition = (upper.index(letter) + k)
newCipher = newCipher + upper[indexPosition]
else:
indexPosition = upper.index(letter) + k
newCipher = newCipher + upper[indexPosition]
elif letter in lower:
if lower.index(letter) + k > 25:
indexPosition = (lower.index(letter) + k)
newCipher = newCipher + lower[indexPosition]
else:
indexPosition = lower.index(letter) + k
newCipher = newCipher + lower[indexPosition]
else:
newCipher = newCipher + letter
return newCipher
f = open('dictionary.txt', "r")
dictionary = set()
for line in f:
word = line.strip()
dictionary.add(word)
print dictionary
#main file
#reading file and encrypting text
f = open('encrypted.txt')
string = ''
out = open("plain1.txt", "w")
#working through each line
for line in f:
for k in range(26):
line = [CaeserCipher(word, k) for word in line]
print line
#listSplit = re.split('[,\[\]]', line)
#print listSplit
string = ("".join(line))
listOfWords = string.split()
lowercase_line = [word.lower() for word in listOfWords]
out.write(dictionary.intersection(lowercase_line))
f.close()
out.close()
本字典的值始終爲空字符串?如果是這樣,你有沒有考慮過使用一套? – Kevin
有沒有一種更有效的方法來代替「」鍵? @Kevin – user3491255
你在問最初如何構建集合?那麼,你應該有'dictionary = set()'而不是'dictionary = {}','dictionary.add(word)'而不是'dictionary [word] =「」' – Kevin