python中對比數組長度
The __len__() is a method on container types. However, python also provides another option of retrieving the length of an array, using the method len().
__len __()是關于容器類型的方法。 但是,python還使用len()方法提供了另一個檢索數組長度的選項。
The len(abc) usually ends up calling abc.__len__().
len(abc)通常最終會調用abc .__ len __() 。
Example usage of len() method is as follows,
len()方法的示例用法如下,
# array declaration
test_arr = [1,2,3]
# length
print(len(test_arr))
print(test_arr.__len__())
Output
輸出量
3
3
The len() method works on a tuple, string (which are array of characters) in similar way.
len()方法以類似的方式處理元組,字符串(字符數組)。
test_tuple = (1,2,3)
# length
print(len(test_tuple))
# string
test_str = 'include-help'
# length
print(len(test_str))
Output
輸出量
3
12
翻譯自: https://www.includehelp.com/python/preferred-way-to-retrieve-the-length-of-an-array.aspx
python中對比數組長度