len() 用于统计序列的长度,字符串 、元组 、列表都属于序列
In [1]: str = "hello world" In [2]: len(str) Out[2]: 11
len函数python
Python len() function returns the length of the object. Generally, len() function is used with sequences (string, tuple) and collections (dict, set) to get the number of items.
Python len()函数返回对象的长度。 通常,len()函数与序列(字符串, 元组 )和集合( dict , set )一起使用以获取项数。
Python len()函数 (Python len() function)
Python len() function returns the length of the object. This function internally calls __len__() function of the object. So we can use len() function with any object that defines __len__() function.
Python len()函数返回对象的长度。 此函数在内部调用对象的__len __()函数。 因此,我们可以将len()函数与任何定义__len __()函数的对象一起使用。
Let’s look at some examples of using len() function with built-in sequences and collection objects.
让我们看一些将len()函数与内置序列和集合对象一起使用的示例。
# len with sequence print('string length =', len('abc')) # string print('tuple length =', len((1, 2, 3))) # tuple print('list length =', len([1, 2, 3, 4])) # list print('bytes length =', len(bytes('abc', 'utf-8'))) # bytes print('range length =', len(range(10, 20, 2))) # range # len with collections print('dict length =', len({"a": 1, "b": 2})) # dict print('set length =', len(set([1, 2, 3, 3]))) # set print('frozenset length =', len(frozenset([1, 2, 2, 3]))) # frozenset
Output:
输出:
string length = 3 tuple length = 3 list length = 4 bytes length = 3 range length = 5 dict length = 2 set length = 3 frozenset length = 3
Python len()对象 (Python len() Object)
Let’s define a custom class with __len__() function and call len() function with it’s object as argument.
让我们使用__len __()函数定义一个自定义类,并以其对象作为参数调用len()函数。
class Employee: name = '' def __init__(self, n): self.name = n def __len__(self): return len(self.name) e = Employee('Pankaj') print('employee object length =', len(e))
Output:
输出:
employee object length = 6
If we remove __len__() function from Employee object, we will get the following exception.
如果我们从Employee对象中删除__len __()函数,我们将得到以下异常。
TypeError: object of type 'Employee' has no len()
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。Reference: Official Documentation
参考: 官方文档
len函数python
int Get_Len(Node * h)
{
if (h == NULL)
return 0;
Node *tmp = h;
int count = 0;
while (tmp->next)
{
count++;
tmp = tmp->next;
}
return count;
}
python中len函数
Python len()函数 (Python len() function)
len() function is a library function in Python, it is used to get the length of an object (object may a string, list, tuple, etc). It accepts an object and returns its length (total number of characters in case of a string, the total number of elements in case of an iterable).
len()函数是Python中的库函数,用于获取对象的长度(对象可以是字符串,列表,元组等)。 它接受一个对象并返回其长度(如果是字符串,则为字符总数,如果是可迭代的则为元素总数)。
Syntax:
句法:
len(object)
Parameter:object – an object like string, list etc whose length to be calculated.
参数: object –一个对象,例如字符串,列表等,其长度将被计算。
Return value: int – returns length of the object.
返回值: int –返回对象的长度。
Example:
例:
Input: a = "Hello world!" print(len(a)) Output: 12
Python code to get the length of an object (string, list etc)
Python代码获取对象的长度(字符串,列表等)
# python code to demonstrate an example # of len() function a = "Hello world!" # string value b = [10, 20, 30, 40, 50] # list c = ["Hello", "World!", "Hi", "Friends"] # list of strings d = ("Hello", "World!", "Hi", "Friends") # Tuple print("length of a: ", len(a)) print("length of b: ", len(b)) print("length of d: ", len(c)) print("length of c: ", len(d))
Output
输出量
length of a: 12 length of b: 5 length of d: 4 length of c: 4
翻译自: https://www.includehelp.com/python/len-function-with-example.aspx
python中len函数
len() 用于统计序列的长度,字符串 、元组 、列表都属于序列
In [1]: str = "hello world" In [2]: len(str) Out[2]: 11