根据以前的条件更改列表中的值 [英] Change Value in a list based on previous condition

查看:53
本文介绍了根据以前的条件更改列表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个零和一的列表.我试图将0的值1替换为0,如果对于所需的输出,先前的值也是1,如下所示.

I have a list of zeros and ones. I am trying to replace the a value of 1 with a 0 if the previous value is also a 1 for a desired output as shown below.

list =     [1,1,1,0,0,0,1,0,1,1,0]
new_list = [1,0,0,0,0,0,1,0,1,0,0]

我尝试使用for循环无济于事.有什么建议吗?

I've tried using a for loop to no avail. Any suggestions?

推荐答案

此for循环怎么样:

list =     [1,1,1,0,0,0,1,0,1,1,0]
new_list = []

ant=0
for i in list:
    if ant ==0 and i==1:
        new_list.append(1)
    else:
        new_list.append(0)
    ant=i

这篇关于根据以前的条件更改列表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆