a = True if a else False
1
Trim21 2020 年 3 月 3 日 via Android bool(a)………
|
3
laqow 2020 年 3 月 3 日 via Android
只知道
try except if is null |
4
14v45mJPBYJW8dT7 2020 年 3 月 3 日
bool(len(a))
|
5
Levi233 2020 年 3 月 3 日
@freakxx 如果是 0,你用 if 也是一样的结果啊。。。python 的隐式转换了解一下?
其实这种情况我们都不转为 true 或 false,直接 if 就行,动态类型语言不要用静态类型语言的思维去开发 |
6
Dvel 2020 年 3 月 3 日
a = False if a in ['', (), [], set(), {}] else True
是这个意思吗? |
7
mimzy 2020 年 3 月 3 日
你的非空值定义是什么? None False '' [] () {} set() range(0) 以外还有么?
|
8
Trim21 2020 年 3 月 3 日 @freakxx #2
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> assert bool(0) is False, "bool(0) should be `False`" >>> if 0: ... pass ... else: ... print(False) ... False >>> 错在哪了。。。bool 跟 if 的行为不是一样的吗 |
10
qmpz02 2020 年 3 月 3 日 via iPhone
not not a
|
14
weyou 2020 年 3 月 3 日 via Android
跟着 if 走,一楼正解,没啥好说的
|
15
SjwNo1 2020 年 3 月 3 日
a = True if a is not 空 else False (逃
|
16
0ZXYDDu796nVCFxq 2020 年 3 月 3 日
a = True if format(a) else False
测试: >>> a = False >>> True if format(a) else False True >>> a = None >>> True if format(a) else False True >>> a = 0 >>> True if format(a) else False True >>> a = [] >>> True if format(a) else False True >>> a = int >>> True if format(a) else False True |
18
2DaYe 2020 年 3 月 3 日 a = a is not None
|
19
leavic 2020 年 3 月 4 日
a= if a
|
20
lithiumii 2020 年 3 月 4 日 via Android
任意非空值范围太广了吧,包括各种 object 吗?不过感觉我写 pandas 的时候倒是遇到过类似的需求,nan、nat、None 等混一起,这时候会用 pd.isna
|
21
yucongo 2020 年 3 月 6 日
not not any_val
|
22
necomancer 2020 年 3 月 11 日
a = not not a
|