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 :
- The function input parameter type is list , Need to learn Python Features and common functions .
- 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 .
- 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 :
- Count the number of occurrences of each element in the list , You need to traverse the list , use for loop .
- For each element , Record the number of occurrences in the dictionary .
- 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 :
- Manual statistics .
- 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
The sidebar is recommended
- Python collects and monitors system data -- psutil
- Python chat room (Tkinter writing interface, streaming, socket to realize private chat, group chat, check chat records, Mysql to store data)
- Learning this Python library can reduce at least 100 lines of code
- leetcode 67. Add Binary(python)
- Regular re parameter replacement of Python 3 interface automation test framework
- V. pandas based on Python
- Only 15 lines of code is needed for face detection! (using Python and openCV)
- [Python crawler Sao operation] you can crawl Sirius cinema movies without paying
- leetcode 69. Sqrt(x)(python)
- Teach you to read the source code of Cpython (I)
guess what you like
-
Snowball learning started in the fourth quarter of Python. One needs three meals. I have a new understanding of Python functional programming, process-oriented, object-oriented and functional
-
leetcode 88. Merge Sorted Array(python)
-
Don't you know more about a python library before the end of 2021?
-
Python crawler web page parsing artifact XPath quick start teaching!!!
-
Use Python and OpenCV to watermark the image
-
String and related methods of Python data type introduction
-
Heapq module of Python module
-
Introduction to beautiful soup of Python crawler weapon, detailed explanation, actual combat summary!!!
-
Event loop of Python collaboration series
-
Django docking pin login system
Random recommended
- [recalling the 1970s] using Python to repair the wonderful memories of parents' generation, black-and-white photos become color photos
- You used to know Python advanced
- Pyinstaller package Python project
- 2021 IEEE programming language rankings: Python tops the list!
- Implementation of Python automatic test control
- Python advanced: [Baidu translation reverse] graphic and video teaching!!!
- Do you know the fuzzy semantics in Python syntax?
- [Python from introduction to mastery] (XXVII) learn more about pilot!
- Playing excel office automation with Python
- Some applications of heapq module of Python module
- Python and go languages are so popular, which is more suitable for you?
- Python practical skills task segmentation
- Python simulated Login, numpy module, python simulated epidemic spread
- Python opencv contour discovery function based on image edge extraction
- Application of Hoff circle detection in Python opencv
- Python reptile test ox knife (I)
- Day 1: learn the Django framework of Python development
- django -- minio_ S3 file storage service
- [algorithm learning] 02.03 Delete intermediate nodes (Java / C / C + + / Python / go)
- Similarities and differences of five pandas combinatorial functions
- Learning in Python + opencv -- extracting corners
- Python beginner's eighth day ()
- Necessary knowledge of Python: take you to learn regular expressions from zero
- Get your girlfriend's chat records with Python and solve the paranoia with one move
- My new book "Python 3 web crawler development practice (Second Edition)" has been recommended by the father of Python!
- From zero to familiarity, it will take you to master the use of Python len() function
- Python type hint type annotation guide
- leetcode 108. Convert Sorted Array to Binary Search Tree(python)
- For the geometric transformation of Python OpenCV image, let's first talk about the extraordinary resize function
- leetcode 701. Insert into a Binary Search Tree (python)
- For another 3 days, I sorted out 80 Python datetime examples, which must be collected!
- Python crawler actual combat | using multithreading to crawl lol HD Wallpaper
- Complete a python game in 28 minutes, "customer service play over the president card"
- The universal Python praise machine (commonly known as the brushing machine) in the whole network. Do you want to know the principle? After reading this article, you can write one yourself
- How does Python compare file differences between two paths
- Common OS operations for Python
- [Python data structure series] linear table - explanation of knowledge points + code implementation
- How Python parses web pages using BS4
- How do Python Network requests pass parameters
- Python core programming - decorator