在 python 3.2 中,我试图使用字典为字母表中的每个字母分配一个值。模式是'a'=1,'b'=2,'c'=3…'z'=26。我有一个名为 words.txt 的文件,在这个文件中有一长串单词。单词以大写字母开头,但是,我的值仅针对小写字母定义。无论如何,当单词转换为小写时,我必须为每个单词分配一个与其字母值的总和相对应的值。我也知道如何找出列表中有多少单词的总值是 137 的整数倍?我也很困惑如何让 python 引用 .txt 文件。
欢迎任何帮助!谢谢你!
这是我到目前为止的代码:
d = {'a':1,'b':2,'c':3,'d':4,'e':5,'f':6,'g':7,'h':8,'i':9,'j':10,'k':11,'l':12,'m':13,'n':14,'o':15,'p':16,'q':17,'r':18,'s':19,'t':20,'u':21,'v':21,'w':23,'x':24,'y':25,'z':26}
find = open("words.txt")
[x.lower() for x in ["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"]]
['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']
def num_multiple():
for line in find:
if line.find("word % 137 == 0") == -1:
return line
else:
word = line.strip()
print(num_multiple)
print(len(num_multiple))