gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 16:49:47 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com