Python List Problem Questions

Asked 2 weeks ago, Updated 2 weeks ago, 1 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 4520
android x 1589
java x 1455
javascript x 1363
c x 881
php x 682
html x 629
swift x 586
ios x 538
mysql x 397

© 2022 pinfo. All rights reserved.