简体   繁体   中英

problems in my list sorting code in python

I've come up with this code to sort out duplicates in a randomly arranged list of numbers.

counter = 0
randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []
for x in randomDup_list:
    if len(randomDup_list) == 0:
        dup_sorted.append(x)
        counter +=1
    elif x != randomDup_list[counter]:
        for y in dup_sorted:
            if y != x:
                dup_sorted.append(x)
print(dup_sorted)

When I run the code there are no errors but the numbers don't seem to be appending to my new list, and it comes out blank like this: [] .

The most pythonic way to do this is with a list comprehension, like so:

dup_sorted = [el for index, el in enumerate(randomDup_list) if el not in randomDup_list[:index]]

Enumerate will create a list of tuples with the first tuple element as the index in the list, [(0,0), (1,3), (2,0), ...] are the first 3 elements in your case.

Then it basically checks if el is the first occurence of el in the list and if it is, it adds el to the dup_sorted list.

List comprehensions are maybe hard to understand, but there is plenty of information about them on the internet. Good luck with learning Python!

I did not understand what you want but you can basically sort the list like that

print(sorted(randomDup_list))

and you can create your new list like that

dup_sorted = sorted(randomDup_list)
print(dup_sorted)

Hey welcome to learning python. I have been coding for a couple of years but I still use print to test my logic.

Lets break down your code and add print to each logic test

counter = 0
randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []
for x in randomDup_list:
    print("start iteration")
    if len(randomDup_list) == 0:
        print("1 true")
        dup_sorted.append(x)
        counter +=1
    elif x != randomDup_list[counter]:
        print("2 true")
        print(dup_sorted)
        for y in dup_sorted:
            if y != x:
                print("3 true")
                dup_sorted.append(x)
    print("stop iteration")
print(dup_sorted)

If you run this code you will see that the first logic test will never be true in your example. So it will go to the second logic test. This will eventually evaluate to true but there will be some iterations that will be skipped. Blank blocks of start iteration stop iteration . For the last logic test this will never be true because dup_sorted will always be empty. So for y in dup_sorted will result to nothing.

Also I think sort out is ambiguous. Is it sort? filter?

You can do set(randomDup_list) for filtering out duplicates
You can do sorted(randomDup_list) for sorting out the list
You can do sorted(list(set(randomDup_list)) for a sorted filtered list

Seeing your new comments

randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []
for x in randomDup_list:
    if x in dup_sorted:
        continue
    else:
        dup_sorted.append(x)
print(dup_sorted)

This initializes an empty list. Loops through your list. Check if it exists then appends if not. Since List maintain order, you can expect the order not to change.

To remove any duplicates from the list without changing the order

Code

dup_sorted = []
for x in randomDup_list:
  if not x in dup_sorted:  # check if x is in output yet
    dup_sorted.append(x)   # add x to output
  
print(dup_sorted)
# Output: [0, 3, 1, 9, 8, 2, 4, 5, 6, 7]

You can use this:

counter = 0
randomDup_list = [0, 3, 0, 1, 9, 8, 2, 3, 4, 2, 4, 3, 5, 6, 0, 6, 5, 2, 6, 6, 7, 8, 9, 4, 4]
dup_sorted = []


dup_sorted.append(randomDup_list[0])
for x in range(len(randomDup_list)):
    temp = 0
    for y in range(len(dup_sorted)):
        if randomDup_list[x] != dup_sorted[y]:
            temp = temp + 1
    if temp == len(dup_sorted):
        dup_sorted.append(randomDup_list[x])
        
print(dup_sorted)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM