如何在python中反转列表中的所有布尔值? [英] How to reverse all booleans in the list in python?

查看:515
本文介绍了如何在python中反转列表中的所有布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想像这样反转列表中的所有布尔值:

I want to reverse all my booleans in the list like this:

a=[True,False,True]

对此:

b=[False,True,False]

我尝试过

not a

但这对我来说只不过是假.

But it gives only False to me.

推荐答案

您可以使用列表推导和not所有项目

You could use list comprehension and not all the items

a=[True,False,True] 
b=[not c for c in a]

not a提供了False,因为它检查列表为空,因为它没有返回True,所以将其转换为false

not a provided False due to the fact it checks list is empty since it isn't returns True so it is converted into false

a=[True]
not a
False
a=[False]
not a
True

这篇关于如何在python中反转列表中的所有布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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