Python 类型错误:';dict';对象不可调用

Python 类型错误:';dict';对象不可调用,python,dictionary,Python,Dictionary,我试图循环输入字符串的元素,并从字典中获取它们。我做错了什么 number_map = { 1: -3, 2: -2, 3: -1, 4: 1, 5: 2, 6: 3 } input_str = raw_input("Enter something: ") strikes = [number_map(int(x)) for x in input_str.split()] Strokes=[number_map[int(x)]表示输入分割()中的x 您使用这些括号从dict中获取元素,而不是这

我试图循环输入字符串的元素,并从字典中获取它们。我做错了什么

number_map = { 1: -3, 2: -2, 3: -1, 4: 1, 5: 2, 6: 3 }
input_str = raw_input("Enter something: ")
strikes = [number_map(int(x)) for x in input_str.split()]

Strokes=[number_map[int(x)]表示输入分割()中的x 您使用这些括号从dict中获取元素,而不是这些括号,它是数字映射[int(x)],您试图用一个参数调用映射,您需要使用:

number_map[int(x)]

注意方括号

访问给定键的dict的语法是
number\u map[int(x)]
number\u-map(int(x))
实际上是一个函数调用,但由于
number\u-map
不是可调用的,因此引发了一个异常

strikes  = [number_map[int(x)] for x in input_str.split()]

使用方括号浏览词典。

使用方括号访问词典

strikes = [number_map[int(x)] for x in input_str.split()]

您需要使用
[]
来访问字典的元素。不是
()


更实用的方法是使用
dict.get

input_nums = [int(in_str) for in_str in input_str.split())
strikes = list(map(number_map.get, input_nums.split()))
可以看出,转换有点笨拙,最好使用以下抽象:

显然,在Python3中,可以避免显式转换为
列表
。在Python中可以找到一种更通用的函数组合方法

(备注:我是从Udacity班来这里写的:)


从您给出的示例来看,数组似乎更适合此任务。对于我的情况,这是正确的。
strikes = [number_map[int(x)] for x in input_str.split()]
  number_map = { 1: -3, 2: -2, 3: -1, 4: 1, 5: 2, 6: 3 }
input_str = raw_input("Enter something: ")
strikes = [number_map[int(x)] for x in input_str ]
input_nums = [int(in_str) for in_str in input_str.split())
strikes = list(map(number_map.get, input_nums.split()))
def compose2(f, g):
    return lambda x: f(g(x))

strikes = list(map(compose2(number_map.get, int), input_str.split()))

Example:

list(map(compose2(number_map.get, int), ["1", "2", "7"]))
Out[29]: [-3, -2, None]
def word_score(word):
    "The sum of the individual letter point scores for this word."
    return sum(map(POINTS.get, word))