前言
Python中的 True和 False总是让人困惑,一不小心就会用错,本文总结了三个易错点,分别是逻辑取反、if条件式和pandas.DataFrame.loc切片中的条件式。
1.True和False的逻辑取反
在对True和False进行逻辑取反时,不使用~,而要使用not。
因为在Python中,not才是逻辑取反,而~是按位取反。True和False对应的数值是1和0,~True就相当于对1按位取反,结果是-2,not True的结果才是False。
1
2
3
|
print ( True ) print (~ True ) print ( not True ) |
结果是:
True
-2
False
类似的,~False的结果是1,not False 的结果才是True
1
2
3
|
print ( False ) print (~ False ) print ( not False ) |
结果是:
False
-1
True
注:Python中 ~ 按位取反是按照数的补码取反,即:
1 => 补码00000001 => ~按位取反 => 补码11111110 => 2
双重否定的结果是这样的
1
2
3
4
|
print ( not not True ) print (~~ True ) print ( not ~ True ) print (~( not True )) |
结果为:
True
1
False
-1
对False的双重否定
1
2
3
4
|
print ( not not False ) print (~~ False ) print ( not ~ False ) print (~( not False )) |
结果为:
False
0
False
-2
2.if条件语句中的True和False
Python语言中,if后任何非0和非空(null)值为True,0或者null为False。这点和其他语言不相同,使用多种编程语言时很容易混淆。所以即使判断条件是一个负数,也是按照True处理,不会执行else分支。来看例子:
1
2
3
4
|
if ( - 2 ): print ( 'a' ) else : print ( 'b' ) |
结果为:a
如果使用了~对True或False取反,则得不到想要的结果:
1
2
3
4
|
if (~ True ): # ~True == -2 print ( 'a' ) else : print ( 'b' ) |
结果为:a
只有用not来取反,才能达到逻辑取反的效果:
1
2
3
4
|
if not True : print ( 'a' ) else : print ( 'b' ) |
结果为:b
3.pandas.DataFrame.loc 中的否定
pandas.DataFrame.loc 官方文档中是这么说的
Access a group of rows and columns by label(s) or a boolean array.
可以使用布尔列表作为输入,包括使用一个条件式来返回一个布尔列表,例:
首先创建一个DataFrame
1
2
3
4
5
6
7
|
import pandas as pd df = pd.DataFrame([[ 1 , 2 ], [ 4 , 5 ], [ 7 , 8 ]], index = [ 'cobra' , 'viper' , 'sidewinder' ], columns = [ 'max_speed' , 'shield' ]) df |
使用条件式来筛选出shield大于6的数据
1
|
df.loc[df[ 'shield' ] > 6 ] |
筛选出shield域小于等于6的数据,可以
1
|
df.loc[df[ 'shield' ] < = 6 ] |
也可以用
1
|
~ df.loc[~(df[ 'shield' ] > 6 )] |
另一个例子,筛选出index中不包含er两个字母的数据
1
|
df.loc[~df.index. str .contains( 'er' )] |
需要注意的是,在这里使用df.index.str.contains('er')作为条件筛选时,返回的是pd.Series。
而在pd.Series中, ~操作符重载了,它对布尔类型数据和对数值类型数据的处理分别是逻辑取反和按位取反。
1
|
df.index. str .contains( 'er' ) |
的结果是:
array([False, True, True])
对布尔类型的pd.Series使用~取反,是逻辑取反
1
|
~pd.Series([ False , True , False ]) |
结果为
True
False
True
dtype: bool
而如果对数值型的pd.Series使用~取反,则是按位取反
1
|
~pd.Series([ 1 , 2 , 3 ]) |
结果为
-2
-3
-4
dtype: int64
总结
到此这篇关于Python中True(真)和False(假)判断的文章就介绍到这了,更多相关Python True和False详解内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_41996102/article/details/119538705