2024-04-24 00:07:40 发布
网友
例如,如果通过了以下操作:
a = []
如何检查a是否为空?
a
我更愿意明确地说:
if len(li) == 0: print('the list is empty')
这样就可以100%地确定li是一个序列(列表),我们想测试它的大小。我对if not li: ...的问题是,它给人的错误印象是li是一个布尔变量。
li
if not li: ...
python方法是从PEP 8 style guide(其中是表示“推荐”,而否表示“不推荐”):
For sequences, (strings, lists, tuples), use the fact that empty sequences are false. Yes: if not seq: if seq: No: if len(seq): if not len(seq):
For sequences, (strings, lists, tuples), use the fact that empty sequences are false.
Yes: if not seq: if seq: No: if len(seq): if not len(seq):
if not a: print("List is empty")
使用空的list的implicit booleanness是相当的python。
list
我更愿意明确地说:
这样就可以100%地确定
li
是一个序列(列表),我们想测试它的大小。我对if not li: ...
的问题是,它给人的错误印象是li
是一个布尔变量。python方法是从PEP 8 style guide(其中是表示“推荐”,而否表示“不推荐”):
使用空的
list
的implicit booleanness是相当的python。相关问题 更多 >
编程相关推荐