Python List Problem Questions

Asked 2 years ago, Updated 2 years ago, 18 views

x=[20, 10, 20, 30, 10, 30, 10] #xlist, number 70-6
x.sort()#SortListSort
print(x)#Output
L=len(x)#length function
i=-1
while i<L:Repeat #7
    i+=1
    c=x.count(x[i]) #0th-6th term overlapping
    if c>1: # If there is an overlap
        For kin range (c-1) : # Clear everything except one
            x.remove(x[i])
    print(x) #ResultOutput

The code is made like this and the result comes out like this.

[10, 10, 10, 20, 20, 30, 30]
[10, 20, 20, 30, 30]
[10, 20, 30, 30]
[10, 20, 30]
Traceback (most recent call last):
  File "C:/Users/user/Desktop/rensyu.py", line 17, in <module>
    c=x.count(x[i]) #0th-6th term overlapping cleared
IndexError: list index out of range

What should I do to get [10, 20, 30] only?

python

2022-09-20 10:27

1 Answers

x = [1, 2, 3, 2, 3, 4]
x.sort()
x1 = []
for i in x:
    if i not in x1:
        x1.append(i)
print(x1)


2022-09-20 10:27

If you have any answers or tips

Popular Tags
python x 4647
android x 1593
java x 1494
javascript x 1427
c x 927
c++ x 878
ruby-on-rails x 696
php x 692
python3 x 685
html x 656

© 2024 OneMinuteCode. All rights reserved.