【发布时间】:2013-01-16 05:24:58
【问题描述】:
Python for 循环没有从列表中迭代“0”!
我尝试编写代码将输入分隔为数字和字母(或运算符):
g='10+10+20x'
t=[]
for each_g in g:
t.append(each_g)
lol=[]
a=[]
for each_t in t:
if each_t.isdigit():
lol.append(each_t)
x = t.index(each_t)
t.pop(x)
else:
lol = ''.join(lol)
a.append(lol)
a.append(each_t)
lol=[]
print(a)
期望的输出是:
['10', '+', '10', '+', '20', 'x']
但它会打印出来
['1', '+', '1', '+', '2', 'x']
改为。
代码是否有任何问题或更好的解决方案使其按预期工作?
谢谢。
【问题讨论】:
标签: python list for-loop iteration