python在列表中保留for循环中的值

2024-05-29 03:53:28 发布

您现在位置:Python中文网/ 问答频道 /正文

亲爱的

如何将for循环中的值保存到列表中

lista = list([])   

for x in datos[2572:2582]:

    if re.match('\d{3}-(.*?)Todas', (x[0])):
       lista = (x[0] + " FALSE")
       
    elif re.match('\d{3}-', (x[0])):
        lista = (x[0] + " TRUE")
        
    else:
        lista = (x[0] + " FALSE")

我想得到一个列表'lista',其中包含前面代码中的值


Tags: 代码inrefalsetrue列表forif
2条回答

虽然这个问题不是很清楚,但我想这可能就是你想要的

lista=[]
for x in datos[2572:2582]:

    if re.match('\d{3}-(.*?)Todas', (x[0])):
       lista.append(x[0] + " FALSE")
       
    elif re.match('\d{3}-', (x[0])):
        lista.append(x[0] + " TRUE") 

    else:
        lista.append(x[0] + " FALSE")

append()将元素添加到列表中。例如,lista.append(x[0] + " FALSE")

相关问题 更多 >

    热门问题