【问题标题】:python 3.2-using dictionary to assign each letter in alphabet a numeric value&finding the sum of words in a .txt file based on their letter valuespython 3.2-使用字典为字母表中的每个字母分配一个数值并根据它们的字母值在.txt文件中查找单词的总和
【发布时间】:2013-03-24 05:14:59
【问题描述】:

在 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))

【问题讨论】:

  • 快速方法是d = dict(zip(string.ascii_lowercase, range(1, 27)))
  • @jamylak 这可以节省很多时间!但是,Python 告诉我“未定义名称字符串”。我应该换东西吗?谢谢!!!
  • @jamylak 这可以节省很多时间!但是,Python 告诉我“未定义名称字符串”。我应该换东西吗?谢谢!!!
  • @jamylak 再次感谢您的帮助!
  • @jamylak 你知道如何用下面的代码解决问题吗?非常感谢! Python 知道使用我的“d”吗?我在您编写的代码正上方的原始评论中复制了我发布的列表(现在已更改为单行版本),Python 不断返回 values.append(d[letter_lower ]) 我究竟做错了什么? d 应该放在别处吗?

标签: python list dictionary type-conversion python-3.2


【解决方案1】:

我在这里看到了一些问题。首先,您使用find 来查找文字字符串"word % 137 == 0" 的结果,而不是计算的结果。

这里有一些东西可以简化你的代码:

values_of_words = [] # all the values for words

with open('words.txt') as the_file:
   for word in the_file:
       word = word.strip() # removes \n from the word
       values = [] # we will store letter values for each word
       for letter in word:
          # convert each letter to lowercase
          letter_lower = letter.lower()

          # find the score and add it to values
          values.append(d[letter_lower])

       # For each word, add up the values for each letter
       # and store them in the list
       values_of_words.append(sum(values))

count = 0
for value in values_of_words:
    if value % 137 == 0:
       count += 1

print("Number of words with values that are multiple of 137: {}".format(count))

【讨论】:

  • 非常感谢! Python 知道使用我的“d”吗?我复制了我在您编写的代码正上方的原始评论中发布的列表,Python 不断返回 values.append(d[letter_lower]) 中的“KeyError: '\n'” 我做错了什么? d 应该放在别处吗?
  • 我刚刚更新了代码,但请记住,如果您的文件有空行,这将不起作用。为此,您必须自己解决 :) 对于您的 d,请参阅 @jamylak 关于如何有效生成它的评论。 d 需要在使用前定义,所以在values_of_words = [] 行附近的某处,添加设置d 的逻辑。
  • 再次感谢!幸运的是没有空行,运行流畅。但我认为我在最初的帖子中错误地描述了这个问题。我真正想知道的是:列表中有多少单词的总值是 137 的整数倍?我们必须首先计算所有值,然后通过 for value 循环运行它们以获取您在上面发布的计数。所以我的(最后一个)Q 将是 - 我如何让 Python 总结每个单词的值,然后确定它是否是倍数。现在它给出的答案是 234202,这对于像 137 这样的随机 # 来说已经很多了。
  • 我希望你能自己发现错误,但我还是更新了代码。
  • 谢谢!我能够发现它,但我认为我无法修复它。也感谢您的精心回复!
【解决方案2】:

您是否考虑过使用 ord() 和 chr() 函数来获取字母的 ASCII 值?

with open('words.txt')as word_file:
    high_score = 0
    for word in word_file:
        word = word.strip()
    value = 0
    for letter in word:
        value += ord(letter) % 97
    if value % 137 == 0:
        high_score += 1
    print('Number of words with values that are a multiple of 137 {}'.format(high_score))

我意识到这与您之前的答案没有什么不同,但如果您的字典非常大,它可能会减少内存开销。此外,能够将字符转换为 ASCII 值并返回可以让您做一些非常酷的事情,尤其是在密码学方面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多