我试图比较一下,看看列表中的单词是字典全部还是单词。我正在编写解码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