【问题标题】:Got an error: 'list' object is not callable in Python [duplicate]出现错误:“列表”对象在 Python 中不可调用 [重复]
【发布时间】:2020-12-23 04:10:46
【问题描述】:

我想根据现有列表 (arr_0) 获取汽车品牌列表 (arr_1)。这是我的代码:

class Keywords:
    def __init__(self):
        self.brand = ["kia", "hyundai", "mitsubishi", "honda", "toyota", "ford", "nissan", "suzuki", "vinfast", "mazda"]
    def classifyOldKeywords(self, arr_0, type):
        arr_copy = []
        arr_copy = [arr_0[i] for i in range(len(arr_0))]
        arr_for_type = []
        [arr_for_type.insert(0, arr_copy.pop(i)) for i in range(len(arr_copy)-1, -1, -1) if (arr_copy[i].lower() in type)]
        return arr_for_type
arr_0 = ['VinFast', 'Lux A', 'VinFast', 'Lux A', 'VinFast', 'VinFast', 'Lux A', 'VinFast', 'VinFast', 'Lux A', 'VinFast', 'Lux A', 'VinFast', 'sedan', 'VinFast', 'VinFast', 'Fadil', 'Lux A', 'Lux SA', 'Lux A']
arr_1 = Keywords().classifyOldKeywords(arr_0, Keywords().brand)
print(arr_1)

它返回如下错误:

Traceback (most recent call last):
  File "final.py", line 80, in <module>
    arr_copy = [arr_0[i] for i in range(len(arr_0))]
TypeError: 'list' object is not callable

感谢您的帮助。

【问题讨论】:

  • 正如在另一个问题下的评论中已经说明的那样,这肯定是因为lenrange 之前已用作列表的名称。由于没有相关代码,我们无法提供更多信息。

标签: python list class


【解决方案1】:

在这种情况下使用类有什么特别的原因吗? 我看到它的方式(如果我错了,请纠正我):

  • 您有汽车清单
  • 您有一个汽车品牌列表
  • 您想要打印一个列表,其中包含与品牌列表中的汽车相匹配的汽车

如果是这样,这可能是一种方法:

carList = ["VinFast", "Lux A", "VinFast", "Lux A", "VinFast", "VinFast", "Lux A", "VinFast", "VinFast", "Lux A", "VinFast", "Lux A", "VinFast", "sedan", "VinFast", "VinFast", "Fadil", "Lux A", "Lux SA", "Lux A"]
brandList = ["kia", "hyundai", "mitsubishi", "honda", "toyota", "ford", "nissan", "suzuki", "vinfast", "mazda"]
matchList = []

[ matchList.append(car) for car in carList if car.lower() in map(str.lower, brandList) ]

# If brandList is always lowercase
# [ matchList.append(car) for car in carList if car.lower() in brandList ]

print(matchList)

【讨论】:

    【解决方案2】:

    你可以用这个:

    arr_copy = arr_0.copy()

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 2012-12-06
      • 1970-01-01
      • 2017-03-27
      • 2019-02-06
      • 2017-02-19
      • 2023-04-11
      • 2017-11-15
      • 1970-01-01
      相关资源
      最近更新 更多