Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 字典中项目的总和_Python 3.x - Fatal编程技术网

Python 3.x 字典中项目的总和

Python 3.x 字典中项目的总和,python-3.x,Python 3.x,我一直在试着做运动。。目标是对项目进行求和,看看哪个项目的值最高,然后返回,每个字母对应一个值。例如,“Babel”值10分(3+1+3+1+2),“Xadrez”值21分(8+1+2+1+1+8),因此程序应该返回“Xadrez”。 我的代码是: def better(l1): dic = {'D':2, 'C':2, 'L':2, 'P':2, 'B':3, 'N':3, 'F':4, 'G':4, 'H':4, 'V':4, 'J':5, 'Q':6, '

我一直在试着做运动。。目标是对项目进行求和,看看哪个项目的值最高,然后返回,每个字母对应一个值。例如,“Babel”值10分(3+1+3+1+2),“Xadrez”值21分(8+1+2+1+1+8),因此程序应该返回“Xadrez”。 我的代码是:

    def better(l1):
      dic = {'D':2, 'C':2, 'L':2, 'P':2, 'B':3, 'N':3, 'F':4, 'G':4,
       'H':4, 'V':4, 'J':5, 'Q':6, 'X':8, 'Y':8, 'Z':8}

      for word in dic.keys():
        l1 = []
        best = 0
        sum = 0
        word = word.split()
        word = word.item()

        sum = word.item()

        best = word
        l1 = l1.append(word)
     return best
我的想法是尝试将每个单词拆分,并对每个单词中每个字母的值求和。谢谢
另一个例子:(['ABACO','UTOPIA','ABADE'])>>'ABACO'

我会首先为每个字母分配一个分值。因为每个字母都有一个指定的值,所以这使得给单词打分更容易

points = \
  { 0: '_' # wild letter
  , 1: 'eaionrtlsu'
  , 2: 'dg'
  , 3: 'bcmp'
  , 4: 'fhvwy'
  , 5: 'k'
  , 8: 'jx'
  , 10: 'qz'
  }
您可以像以前一样轻松构建
dic
。最好在评分函数之外定义它,因为
dic
可以重复使用,而不是在每次运行该函数时重新创建(就像在代码中一样)

由于内置的
sum
功能,使用
dic
为单词打分很容易

def score_word (word):
  return sum (dic[letter] for letter in word)

print (score_word ("babel")) # 9
print (score_word ("xadrez")) # 23
现在我们需要一个
max_word
函数,它可以确定两个单词的“max”,即
w1
w2

def max_word (w1, w2):
  if (score_word (w1) > score_word (w2)):
    return w1
  else:
    return w2

print (max_word ("babel", "xadrez")) # xadrez
现在很容易创建一个可以接受任意数量单词的函数,
max\u words

def max_words (w = None, *words):
  if not words:
    return w
  else:
    return max_word (w, max_words (*words))

print (max_words ('abaco', 'utopia', 'abade')) # abaco
print (max_words ()) # None

你似乎有点迷失在你的代码中。您正在中途交换
l1
dic
的角色。慢慢来,再好好想想这不会让我想起,但它确实帮了我很大的忙!我试着只用一个函数。。你的代码行吗?我再次尝试,在def max_words之前,我的操作基本上与您相同,但都在同一个函数中,您介意解释一下“(w=None,*words)”的意思吗?非常感谢@user633183将所有内容合并到单个函数中是无效的。一切可能的情况都更糟。在Python中,每个文件都已经是一个模块,为您提供了一个干净的范围来组织程序。wrt
(w=None,*words)
w=None
是第一个参数
w
,如果不提供参数,将为其分配一个默认值
None
*words
是一个rest参数,它收集单个数组中的其余参数,
words
。例如,
max_words(“a”、“b”、“c”)
w=“a”
words=[“b”、“c”]
但是在这个表达式中,
max_words(*words)
*words
是一个扩展参数,它将一个iterable值转换为多个参数。例如,当
words=[“b”,“c”]
时,
max\u words(*words)
相当于
max\u words(“b”,“c”)
def max_words (w = None, *words):
  if not words:
    return w
  else:
    return max_word (w, max_words (*words))

print (max_words ('abaco', 'utopia', 'abade')) # abaco
print (max_words ()) # None