t = 8
string = "1 2 3 4 3 3 2 1"
string.replace(" ","")
string2 = [x for x in string]
print string2
for n in range(t-1):
string2.remove(' ')
print string2
def remover(ca):
newca = []
print len(ca)
if len(ca) == 1:
return ca
else:
for i in ca:
newca.append(int(i) - int(min(ca)))
for x in newca:
if x == 0:
newca.remove(0)
print newca
return remover(newca)
print (remover(string2))
它应该是一个程序,接收一个数字列表,列表中的每一个数字都从中减去min(list)。它在最初的几次迭代中工作得很好,但在接近结束时就不行了。我在这里和那里添加了打印语句以帮助解决问题。你知道吗
编辑:
t = 8
string = "1 2 3 4 3 3 2 1"
string = string.replace(" ","")
string2 = [x for x in string]
print len(string2)
def remover(ca):
newca = []
if len(ca) == 1: return()
else:
for i in ca:
newca.append(int(i) - int(min(ca)))
while 0 in newca:
newca.remove(0)
print len(newca)
return remover(newca)
print (remover(string2))
(不是真正的答案,JFYI:)
如果你把程序分解成适当的部分,你的程序可能会短得多。你知道吗
像往常一样,上述模式可以替换为理解:
试试看:
请注意,没有项目是如何被删除的,而且数据通常不会发生任何变化。这种方法对程序有很大的帮助;试试看。你知道吗
所以如果我理解了你所期望的描述
我相信下面的脚本会让你更接近你的目标。你知道吗
逻辑:
迭代一个列表并同时从中删除内容可能会导致奇怪和意外的behvaio。尝试改用
while
循环。你知道吗或列表:
或者创建另一个临时列表:
相关问题 更多 >
编程相关推荐