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
你大概想要这个:
这将为每个外部循环迭代(列表中的每个单词)打印剩余的公共前缀
相关问题 更多 >
编程相关推荐