P104: Find the number of elements of a list
求列表中元素数量。用单元测试描述为:
from python99.lists.p104 import length, length_recursive
def test_length():
assert length([1, 3, 4, 5, 'Apple', 'Orange']) == 6
assert length(None) == 0
Python内建了函数len
,用于读取sequence
类型对象的长度。list
就是一类sequence
。使用函数len
可以直接读取list
的长度。
## Find the number of elements of a list
def length(list):
if list is None:
return 0
return len(list)