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
x = [1, 2, 3, 2, 3, 4]
x.sort()
x1 = []
for i in x:
if i not in x1:
x1.append(i)
print(x1)
© 2024 OneMinuteCode. All rights reserved.