Python coding, operation error (novice)

Asked 2 years ago, Updated 2 years ago, 144 views

ex. [1,2,2,4,5]-> [2,2] // [1,4,4,4,6,8,8]->[4,4,4,8,8] // [1,2,3,4,5] ->[]

(Questionnaire: You are given a non-empty list of interiors (X). For this task, you should return a list consisting of only the non-unique elements in this list. To do so you will need to remove all unique elements (elements which are contained in a given list only once). When solving this task, do not change the order of the list. Example: [1, 2, 3, 1, 3] 1 and 3 non-unique elements so the result will be [1, 3, 1, 3].// Problem source: Empire of code)

Questions

Thank you for reading this long article.

def non_unique(data):
    result = data

    for i in data:        
        if data.count(i) == 1:
            result.remove(i)
        else:
            n=0

    return result

#The contents below are irrelevant to the coding contents.

if __name__ == "__main__":
    # # These "asserts" using only for self-checking and not necessary for auto-testing
    # # Rank 1
    assert isinstance(non_unique([1]), list), "The result must be a list"
    assert non_unique([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"
    assert non_unique([1, 2, 3, 4, 5]) == [], "2nd example"
    assert non_unique([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"
    assert non_unique([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"

    # # Rank 2
    assert non_unique(['P', 7, 'j', 'A', 'P', 'N', 'Z', 'i',
                       'A', 'X', 'j', 'L', 'y', 's', 'K', 'g',
                       'p', 'r', 7, 'b']) == ['P', 7, 'j', 'A', 'P', 'A', 'j', 'p', 7], "Letters"

    print("All done? Earn rewards by using the 'Check' button!")


python remove

2022-09-22 19:39

2 Answers

This is a deep copy problem. Please refer to the link below http://add2paper.github.io/2014/12/01/More-Python-1-Copy/

You can use the pass as shown below.

if 1:
    print "Hello"
else :
    pass


2022-09-22 19:39

If you erase an element with a count of 1 from the front, a problem will occur.

Dear author, may I ask

data= [1, 2, 3, 4]

for i in data: 
    data.remove(i)

Do you think all elements in the data list are deleted when you run?

In practice, only the elements in the odd index are deleted.

The following are example code and execution results.

<code>

data = [10, 20, 30, 40]

for i in data:
    print ('data list = ', data)
    print ('i value = ', i, '\n')
    data.remove(i)

print('final data list: ', data)
<Results>

data list = [10, 20, 30, 40]
i value = 10

data list = [20, 30, 40]
i value = 30

Final data list: [20, 40]

After the first remove(i) removes 10, i becomes 30 instead of 20. We wanted i to be the second factor in the initial data list, 20, but Because 10 was removed by remove(), and the second factor in the list was changed to 30.

So, I saved the index of the element with count 1 in the list, called reverse() to erase it from the back of the list, and deleted it.

import copy

def f(data):

    res = copy.deepcopy(data)
    del_list = []
    index = 0

    for i in res:
        if res.count(i) == 1:
            del_list.append(index)        
        index += 1

    del_list.reverse()

    for i in del_list:
        del res[i]

    return res

There might be a simpler way to solve it.


2022-09-22 19:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.