如何在python中获取列表列表的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20151141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get length of a list of lists in python
提问by user3022562
So, if I have a list called myList I use len(myList)to find the number of elements in that list. Fine. But how do I find the number of lists in a list?
因此,如果我有一个名为 myList 的列表,我会使用它len(myList)来查找该列表中的元素数量。美好的。但是如何找到列表中的列表数量?
text = open("filetest.txt", "r")
myLines = text.readlines()
numLines=len(myLines)
print numLines
The above text file used has 3 lines of 4 elements separated by commas. The variable numLines prints out as '4' not '3'. So, len(myLines)is returning the number of elements in each list not the length of the list of lists.
上面使用的文本文件有 3 行,每行 4 个元素,用逗号分隔。变量 numLines 打印为“4”而不是“3”。因此,len(myLines)返回每个列表中的元素数而不是列表列表的长度。
When I print myLines[0]I get the first list, myLines[1]the second list, etc. But len(myLines)does not show me the number of lists, which should be the same as 'number of lines'.
当我打印时,myLines[0]我得到第一个列表、myLines[1]第二个列表等。但len(myLines)没有显示列表的数量,这应该与“行数”相同。
I need to determine how many lines are being read from the file.
我需要确定从文件中读取了多少行。
回答by duhaime
"The above text file used has 3 lines of 4 elements separated by commas. The variable numLines prints out as '4' not '3'. So, len(myLines) is returning the number of elements in each list not the length of the list of lists."
“上面使用的文本文件有 3 行,每行 4 个元素,由逗号分隔。变量 numLines 打印为 '4' 而不是 '3'。因此,len(myLines) 返回的是每个列表中的元素数,而不是列表的长度名单清单。”
It sounds like you're reading in a .csv with 3 rows and 4 columns. If this is the case, you can find the number of rows and lines by using the .split() method:
听起来您正在阅读具有 3 行 4 列的 .csv。如果是这种情况,您可以使用 .split() 方法找到行数和行数:
text = open("filetest.txt", "r").read()
myRows = text.split("\n") #this method tells Python to split your filetest object each time it encounters a line break
print len(myRows) #will tell you how many rows you have
for row in myRows:
myColumns = row.split(",") #this method will consider each of your rows one at a time. For each of those rows, it will split that row each time it encounters a comma.
print len(myColumns) #will tell you, for each of your rows, how many columns that row contains
回答by Javier Castellanos
This saves the data in a list of lists.
这会将数据保存在列表列表中。
text = open("filetest.txt", "r")
data = [ ]
for line in text:
data.append( line.strip().split() )
print "number of lines ", len(data)
print "number of columns ", len(data[0])
print "element in first row column two ", data[0][1]
回答by Anoop Singh
if the name of your list is listlenthen just type len(listlen). This will return the size of your list in the python.
如果您的列表名称是,listlen则只需键入len(listlen). 这将返回 Python 中列表的大小。
回答by Anoop Singh
The method len() returns the number of elements in the list.
len() 方法返回列表中元素的数量。
list1, list2 = [123, 'xyz', 'zara'], [456, 'abc']
print "First list length : ", len(list1)
print "Second list length : ", len(list2)
When we run above program, it produces the following result ?
当我们运行上面的程序时,它会产生以下结果?
First list length : 3 Second list length : 2
第一个列表长度:3 第二个列表长度:2
回答by Sharon Tourjeman
You can do it with reduce:
你可以用reduce来做到:
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [], [1, 2]]
print(reduce(lambda count, l: count + len(l), a, 0))
# result is 11

