strs = ["flower","flow","flight","fluea","flfjdkl","f"]
temp = strs[0]
for i in range(1, len(strs)):
for j in range(len(temp)):
if j >= len(strs[i]) or strs[i][j] != temp[j]:
temp = temp[:j]
print(temp)
break
我想temp
在完成if
语句时打印变量。但是,它只会在temp
更改时打印。
例如,这段代码的结果是:
flow
fl
f
但我希望结果是:
flow
fl
fl
fl
f
你大概想要这个:
strs = ["flower","flow","flight","fluea","flfjdkl","f"]
temp = strs[0]
for i in range(1, len(strs)):
for j in range(len(temp)):
if j >= len(strs[i]) or strs[i][j] != temp[j]:
temp = temp[:j]
break
print(temp)
flow
fl
fl
fl
f
这将打印每个外循环迭代(列表中的每个单词)的剩余公共前缀。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句