current position:Home>[Python daily homework] day4: write a function to count the number of occurrences of each number in the incoming list and return the corresponding dictionary.

[Python daily homework] day4: write a function to count the number of occurrences of each number in the incoming list and return the corresponding dictionary.

2022-01-30 06:07:26 Bug play programming

Hello everyone , I am a bug, A normal programmer .

The rapid growth of programmers , It's about practicing , More output , Share more , Multiple links .

But the execution is too poor , Missed a lot of good opportunities .

It's not too late to start , Welcome to pay attention , Witness the growth of an ordinary programmer .

Let's set up a Flag, To participate in Brother Tao talks Python Technology circle Clock in the daily homework of knowledge planet , Hold on first 7 God .

problem :

Day4 Q16: Write a function to count the number of occurrences of each number in the incoming list and return the corresponding Dictionary .

solve :

Solve this problem , Start with the information on the topic first . The keyword is list 、 Numbers 、 Dictionaries .

Analysis can be seen :

  1. The function input parameter type is list , Need to learn Python Features and common functions .
  2. Python The element type of the list , differ Java It needs to be declared in advance , Each element can be a different data type . There are limitations in this question , All elements of the list are numbers . For the convenience of statistics , Suppose all the elements in the list are integer numbers .
  3. The return value of the function is in the dictionary , The dictionary is Python Unique data structure in , Also need to learn .

After learning , Start solving the problem . Their thinking :

  1. Count the number of occurrences of each element in the list , You need to traverse the list , use for loop .
  2. For each element , Record the number of occurrences in the dictionary .
  3. You need to determine whether the element already exists in the key of the dictionary , use if combination list Of contains() Function judgement .

Statistics list The number of occurrences of the element , There are two ways :

  1. Manual statistics .
  2. utilize list Of count Function query .

From the perspective of algorithm , I wonder if there is a more efficient way , Those who understand are welcome to comment and guide .

# Q16:  subject 013: Write a function to count the number of occurrences of each number in the incoming list and return the corresponding Dictionary .

#  Naming habits , Variable 、 Method names are separated by underscores ; Class names are capitalized , Hump named .
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 : Traversing elements , 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}


#  Method 2 : use  list  Counting function of , 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 code 

summary :

Okay ,Day4 Clock out is over , To study the python in , list, dict, for loop ,if The basic usage of judgment .

If you are also learning Python , Welcome and bug Communicate together .

Official account bug Play programming , Play programming together !

copyright notice
author[Bug play programming],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201300607245197.html

Random recommended