python - 类型错误 : 'list' object is not callable: why?

标签 python list

from difflib import get_close_matches

order_output = {
    "initiate ion drive" : "Initiating the ion drive.",
    "run" : "Running",
    "eat" : "Eating",
    "enter coords to" : "Settings coords to:"
}


orders_list = ["initiate ion drive", "eat", "run", "enter coords to"]

def ord_input():
    order = input()
    order1 = get_close_matches(order, orders_list)
    order2 = ''.join(map(order1, order_output))
    if order:               ^#the problem
        print(order_output[order1])
    else:
        print("Don't know this order.")

ord_input()

这是错误:

Traceback (most recent call last):
  File "C:/Python34/order.py", line 16, in <module>
    ord_input()
  File "C:/Python34/order.py", line 11, in ord_input
    order2 = ''.join(map(order1, order_output))
TypeError: 'list' object is not callable

我想将该特定列表字符串转换为常规字符串。

我怎样才能做到这一点?

最佳答案

difflib.get_close_matches 返回一个列表。 map 内置函数将一个函数作为其第一个参数,并将其应用于第二个参数的可迭代对象的每个元素。因此,您尝试将列表作为函数调用,这是一个 TypeError。列表不可调用。

如果目标是索引 order_output 字典,则直接执行即可。

order2 = order_output[order1]

关于python - 类型错误 : 'list' object is not callable: why?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35961510/

相关文章:

python - Django:根据 object.owner 检查请求用户

java - 我需要一个简单的命令行程序来使用 XSL 样式表转换 XML

python - 修改 Python 列表中的所有元组

python - 按内部列表的特定索引对列表列表进行排序

python - 在python中写入输出文件

python - 通过 pyodbc 更新 Oracle DB 挂起

python - 三个序列的最长公共(public)子序列

Python3模拟用另一个函数替换函数

c# - 静态构造函数调用两次

c++ - 列表的迭代器错误