python查找列表中元素
To find the total number of elements in a List in Python – we use the len() method, it is an inbuilt method, it accepts an argument (this list) and returns the total number of elements.
要在Python中查找列表中的元素总数,我们使用len()方法 ,它是一个内置方法,它接受一个参数(此列表)并返回元素的总数。
Syntax:
句法:
len(list_object/list_elements)
Note: As an argument, a list object or a direct list ( [elemen1, element2, ...]) can be passed.
注意:作为参数,可以传递列表对象或直接列表( [elemen1,element2,...] )。
Example 1:
范例1:
# declaring lists
list1 = [10, 20, 30, 40, 50]
list2 = ["Hello", "IncludeHelp"]
# printing list and its length
print("list1: ", list1)
print("list2: ", list2)
print("len(list1): ", len(list1))
print("len(list2): ", len(list2))
# passing a direct list elements to the function
print("Here, elements are: ", len([10,20,30,40,50]))
Output
输出量
list1: [10, 20, 30, 40, 50]
list2: ['Hello', 'IncludeHelp']
len(list1): 5
len(list2): 2
Here, elements are: 5
Example 2:
范例2:
# declaring lists
list1 = [10, 20, 30, 40, 50]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]
# finding the lenght of the lists
# before appending the elements
print("Before appending...")
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))
# appending elements
list1.append(60)
list1.append(70)
list2.append(".com")
list3.append(30)
# finding the lenght of the lists
# after appending the elements
print() # prints new line
print("After appending...")
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))
Output
输出量
Before appending...
list1: [10, 20, 30, 40, 50]
list2: ['Hello', 'IncludeHelp']
list3: ['Hello', 10, 20, 'IncludeHelp']
Elements in list1: 5
Elements in list2: 2
Elements in list3: 4
After appending...
list1: [10, 20, 30, 40, 50, 60, 70]
list2: ['Hello', 'IncludeHelp', '.com']
list3: ['Hello', 10, 20, 'IncludeHelp', 30]
Elements in list1: 7
Elements in list2: 3
Elements in list3: 5
计算列表中元素的出现 (Counting the occurrences of an element in the List)
To count the occurrences of a given element – we use List.count() method, it accepts an argument whose occurrences to be found and returns its occurrences.
要计算给定元素的出现次数–我们使用List.count()方法 ,它接受要查找其出现次数的参数并返回其出现次数。
Example:
例:
# declaring lists
list1 = [10, 20, 30, 40, 50, 10, 60, 10]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]
# printing the list and its elements
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)
print("Elements in list1: ", len(list1))
print("Elements in list2: ", len(list2))
print("Elements in list3: ", len(list3))
# printing the occurrences of an element
print("occurrences of 10 in list1: ", list1.count(10))
print("occurrences of 60 in list1: ", list1.count(60))
print("occurrences of 70 in list1: ", list1.count(70))
print("occurrences of \"Hello\" in list2: ", list2.count("Hello"))
print("occurrences of \"World\" in list2: ", list2.count("World"))
print("occurrences of \"IncludeHelp\" in list3: ", list3.count("IncludeHelp"))
Output
输出量
list1: [10, 20, 30, 40, 50, 10, 60, 10]
list2: ['Hello', 'IncludeHelp']
list3: ['Hello', 10, 20, 'IncludeHelp']
Elements in list1: 8
Elements in list2: 2
Elements in list3: 4
occurrences of 10 in list1: 3
occurrences of 60 in list1: 1
occurrences of 70 in list1: 0
occurrences of "Hello" in list2: 1
occurrences of "World" in list2: 0
occurrences of "IncludeHelp" in list3: 1
翻译自: https://www.includehelp.com/python/find-the-number-of-elements-in-a-list.aspx
python查找列表中元素