current position:Home>Write a program, input several integers (separated by commas) and count the number of occurrences of each integer.

Write a program, input several integers (separated by commas) and count the number of occurrences of each integer.

2022-05-15 07:21:02Python advanced

Hello everyone , I'm Pippi .

One 、 Preface

A few days ago Python The diamond exchange group shared a Python The basic question , Take it out here and share it with you , Let's study together .

Programming , Enter a number of integers ( Integers are separated by commas ), Count the number of occurrences of each integer .

This topic is not difficult , But for beginners , It's still a little difficult , Let's see the solution !

Two 、 To solve the process

Solution 1

In fact, this topic is testing you Python Basic content . The code is as follows :

import collectionsdata = input(">>> ")new_lst = eval(data)frequency = collections.Counter(new_lst)print(frequency)

 Programming , Enter a number of integers ( Integers are separated by commas ), Count the number of occurrences of each integer .

Yes, of course , Beginners must not know Counter This method is , There is indeed a suspicion of cheating .

 Programming , Enter a number of integers ( Integers are separated by commas ), Count the number of occurrences of each integer .

Let's take a look at other methods !

【 Luna 】 answer

The following is also a dictionary count , Go straight to the code , As shown below :

a = eval(input(">>>"))a_count = dict()for i in a:    a_count[i] = a_count.get(i, 0) + 1print(a_count)

 Programming , Enter a number of integers ( Integers are separated by commas ), Count the number of occurrences of each integer .

The dictionary is cleverly used here , And used get Method , With one . If you find it difficult to understand , You can also take a look at the following complex version .

Solution 3

Go straight to the code , As shown below :

data = input(">>> ")new_lst = eval(data)dic = {}for item in new_lst:    if item not in dic:        dic[item] = 1    else:        dic[item] += 1print(dic)

 Programming , Enter a number of integers ( Integers are separated by commas ), Count the number of occurrences of each integer .

This method is pure , And it's easy to understand , Not much to explain .

3、 ... and 、 summary

Hello everyone , I'm Pippi . This article mainly shares a Python Basic questions , Specific analysis and code demonstration are given to solve this problem , altogether 3 A way , Help the fans solve the problem smoothly . I believe there must be other ways , Welcome to try , If there is a good way , Remember to share it with me , I help share in the group , Let's learn and communicate !

Finally thanks 【 Luna 】 The specific analysis and code demonstration are given , thank 【dcpeng】、【Jason】、【 Feng Cheng 】 And others participate in learning and communication .

friends , Practice it quickly ! If in the process of learning , Any problems encountered , Welcome to add my friend , I'll pull you in Python The learning exchange group discusses learning together .

copyright notice
author[Python advanced],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/131/202205102149185736.html

Random recommended