我在python上执行了排序功能,但是我写两次如何修复它的“ 2”呢?
liste = [10,200,5,2,3]
minimal = []
w = 0
try:
sayac = 0
lent= (len(liste)-1)
while True:
sayac += 1
if lent == sayac:
break
else:
listelen = (len(liste)-1)
for i in liste:
if i <= liste[w] and i <= liste[listelen] and i <= liste[int(listelen/2)]:
minimal.append(i)
else:
pass
print(minimal)
lent = (len(minimal)-1)
for i in liste:
if i == minimal[lent]:
liste.remove(i)
else:
pass
except:
print(minimal)
OUT : [2, 3, 2, 5, 10, 200] [2, 3, 2, 5, 10, 200] ...
我的“ liste”值是1,但是它写了两次,我该如何解决?我的大脑现在消失了
答案 0 :(得分:0)
因为在第一步中添加了多个minimal
,但随后仅从liste
中删除了最后一个:
liste = [10,200,5,2,3]
minimal = []
w = 0
try:
sayac = 0
lent= (len(liste)-1)
while True:
sayac += 1
if lent == sayac:
break
else:
listelen = (len(liste)-1)
for i in liste:
if i <= liste[w] and i <= liste[listelen] and i <= liste[int(listelen/2)]:
minimal.append(i)
break ##########this line added
else:
pass
print(minimal)
lent = (len(minimal)-1)
for i in liste:
if i == minimal[lent]:
liste.remove(i)
else:
pass
except:
print(minimal)
此外,我会重新考虑破坏条件,这样可能会更好:
liste = [10,200,5,2,3]
minimal = []
w = 0
try:
sayac = 0
lent= (len(liste)-1)
while True:
sayac += 1
if len(liste)==0: ############condition changed
break
else:
listelen = (len(liste)-1)
for i in liste:
if i <= liste[w] and i <= liste[listelen] and i <= liste[int(listelen/2)]:
minimal.append(i)
break #############this line added
else:
pass
print(minimal)
lent = (len(minimal)-1)
for i in liste:
if i == minimal[lent]:
liste.remove(i)
else:
pass
except:
print(minimal)