Hello everyone, I am bug, an ordinary programmer.
Programmers grow faster by practicing more, exporting more, sharing more, linking more.
But his execution was so poor that he missed a lot of good opportunities.
It’s never too late to start. Welcome to pay attention and witness the growth of an ordinary programmer.
Here is a Flag for you to participate in the daily homework of Tao Ge chat Python technosphere knowledge Planet for 7 days.
Question:
Day4 Q16: Write a function that counts the number of occurrences of each number in the passed list and returns the corresponding dictionary.
Solution:
To solve this problem, start with the information on the topic. The keywords are list, number, and dictionary.
Analysis shows that:
- The input parameter type of the function is a list. You need to learn about Python features and common functions.
- The element types of Python lists, unlike Java, are pre-declared; each element can be a different data type. In this case, all elements of the list are numbers. For statistical purposes, assume that the elements in the list are integer numbers.
- Functions return values using dictionaries, which are Python data structures that need to be learned.
So let’s start to solve the problem. Answer:
- Counting the number of occurrences of each element in the list requires traversing the list with a for loop.
- For each element, the number of occurrences is recorded in the dictionary.
- To determine whether an element already exists in a dictionary key, use the if function combined with the contains() function of list.
There are two ways to count occurrences of list elements:
- Manual statistics.
- Query using the count function of list.
Algorithm point of view, do not know whether there is a more efficient way, understand the big guy welcome comment guidance.
Write a function that counts the number of occurrences of each number in the passed list and returns the corresponding dictionary.
Use an underscore to separate variable and method names. Class names are capitalized and humped.
number_list = [1.1.0.2.3.4.5.2.1.3.5.3.5.1.2.3.5.6.87.8.95.3.4]
Method 1: Iterate over the elements and count the number of occurrences of each element
def count_list_element(array) :
result_return = {}
for i in array:
if result_return.__contains__(i):
result_return[i] = result_return[i] + 1
else:
result_return[i] = 1
return result_return
result = count_list_element(number_list)
print(result)
# {1, 4, 0:1, 2, 3, 3, 5, 4, 2, 5, 4, 6:1, 87:1, 8:1, 95:1}
Count the number of occurrences of each element
def count_list_element_2(array) :
result_return = {}
for i in array:
if not result_return.__contains__(i):
result_return[i] = array.count(i)
return result_return
result_2 = count_list_element_2(number_list)
print(result_2)
# {1, 4, 0:1, 2, 3, 3, 5, 4, 2, 5, 4, 6:1, 87:1, 8:1, 95:1}
Copy the code
Conclusion:
Day4 is over, and you’ve learned the basics of list, dict, for, and if in Python.
If you’re learning Python, you’re welcome to talk to bugs.
Concern public number bug play programming, play programming together!