繁体   English   中英

TypeError: 'int' object 不可调用 python 错误

[英]TypeError: 'int' object is not callable python error

我不明白我在这里做错了什么

  File "one.py", line 353, in containsCount
    count = sum(map(count, filter(lambda find: text in find, _list)))
TypeError: 'int' object is not callable

完整的代码是:

    def containsCount(self, _list, text):
        count = 0
        count = sum(map(count, filter(lambda find: text in find, _list)))
        return count

您实际上错误地使用了map() 据我了解,您想要对包含text_list的所有元素求和。

我不确定是否理解您的方法,我看到了两种可能性:

  1. Map 将_list的元素设为 0 或 1(仅当文本在此元素中时为 1),然后将这些元素相加
  2. 过滤元素以仅保留您想要的元素,并获取生成列表的长度。

无论哪种方式,您都不能很好地使用它们,我建议您查看 map 和 filter 的文档,或有关它们如何工作的教程(例如 w3shools 关于map和 geeksforgeeks 关于filter )。

我对如何解决的建议是变得更简单,并使用生成器:

len([x for x in _list if text in x])

您可以将其与sum()结合使用以优化 memory 的使用:

sum(1 for x in _list if text in x)

如果您想了解更多关于生成器的信息或为什么它优化 memory 的使用或任何其他信息,请在评论中询问我

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM