current position:Home>Python repeated element determination function program

Python repeated element determination function program

2022-04-29 08:00:37Python Xiaobai's counter attack

introduction : I learned from Xiaobai python, In order to test the effect of basic learning , Increase certain promotion , Want to pass the national computer grade examination level 2 python To test basic learning . Of course , Passing the computer secondary level is not the purpose , It's not the end , I just want to pass an exam to test my learning results . In the learning process , Will write the process python The title of the applet is published here , I hope to find people who can learn together to discuss , There are many places that may not be perfect , Please correct . Later learning route , We will continue to test our learning achievements by taking some social exams . If you study at an advanced stage , May participate in some python The game , Or take some projects by yourself , Then I will make a summary of some projects according to my own situation Python The program is shared here .

One 、 The source of this project is : National Computer Rank Examination level two course python Language programming , Chapter six, the second program question 4 and 5 Little topic . Repeat element determination , Write a function , Receive list as parameter , If an element appears more than once in the list , Then return to True, But don't change the value of the original list . At the same time, write a program to call this function and output the test results . The first 5 The topic is to make an upgrade program , Use the non repeatability of the set to adapt the previous program , Get a faster and simpler version .

Two 、 Upload your own program first

1. The primary version of the function program

def repeat(lis):
    item_dic = {}
    for i in lis:
    if len(item_dic)<len(lis):
        return True

s = eval(input(' Please enter a list :'))
print(' The original input list {}'.format(s))

2. Upgraded function program

def repeat2(lis):
    if len(set(lis))<len(lis):
        return True
p = eval(input(' Please enter a list :'))
print(' The original input list {}'.format(p))

3、 ... and 、 Interpret program statements .

Let's first understand the difference between the two functions , It mainly uses the two features of dictionary and collection to collect the input data , Deduplication , Comparing element changes , To determine whether there is repetition .

item_dic = {}
    for i in lis:

The first function , I use a dictionary to count the element frequency of the input list , The duplicate elements are merged , Then by comparing the length of the dictionary with the length of the input list , You can compare whether there are duplicate elements .

if len(set(lis))<len(lis):
The second function , It mainly uses the transformation of sets ,set function , Change the input list into a collection , Use the set de duplication function , To repeat automatically , Then compare the changes between the set elements and the input list elements .

From the number and simplification of two function codes , Using collections may work better .

Four 、 Program running effect

1. The effect of the first program


2. The effect of the second program

5、 ... and 、 Things to pay attention to in the procedure .

An error was reported during the writing process , When getting input data ,input Get string , Need to add eval function , Convert the input data , Turn into python Direct operation data , Keep the list type entered , In this way, subsequent processing can be carried out . 

copyright notice
author[Python Xiaobai's counter attack],Please bring the original link to reprint, thank you.

Random recommended