Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python len()函数显示45个值,而不是它应该显示的90个值,我做错了什么?_Python_String_List_Int - Fatal编程技术网

Python len()函数显示45个值,而不是它应该显示的90个值,我做错了什么?

Python len()函数显示45个值,而不是它应该显示的90个值,我做错了什么?,python,string,list,int,Python,String,List,Int,下面是我的代码,它使用txt文件生成一个列表,打印得很好,显示它包含90个值: read_txt = open("Data.txt", "r") my_list = read_txt.readlines() for values in my_list: if not values.startswith("#"): # ignores the header comment in the text file values = values.rstrip("\n") #

下面是我的代码,它使用txt文件生成一个列表,打印得很好,显示它包含90个值:

read_txt = open("Data.txt", "r")

my_list = read_txt.readlines()

for values in my_list:
    if not values.startswith("#"):  # ignores the header comment in the text file
        values = values.rstrip("\n") # strips \n from end of each value

        print(values)

read_txt.close()

print(len(values))

但是,
print(len(values))
给出了45。

唯一的解释是,len([1,2,'a','b'])不会给出列表中元素的计数,如果您有一个重新定义了u len u函数的类。如果你有空,请报到

  def __len__(self):
      #defined differently

否则问题就在别处:)

我不明白这个问题。当我写a=[1,2,4,'b','c'];len(a);这是“5”。没有打字错误。你能举一个更具体的例子说明你在做什么吗?@jubba请在你的帖子中添加一些代码,这样我们就可以理解你在做什么。”我意识到这是因为有int和string值“不,绝对没有
len
不关心列表中对象的类型您需要提供一些示例数据和代码,以便使问题更具体。我意识到我做错了什么,现在让len()给我一个值,但是该值应该是90,返回45。