Python 在不使用 `not` 命令的情况下检查列表是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16021096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:37:34  来源:igfitidea点击:

Check if list is empty without using the `not` command

pythonlistis-empty

提问by user2240288

How can I find out if a list is empty without using the not command?
Here is what I tried:

如何在不使用 not 命令的情况下确定列表是否为空?
这是我尝试过的:

if list3[0] == []:  
    print("No matches found")  
else:  
    print(list3)

I am very much a beginner so excuse me if I do dumb mistakes.

我是一个初学者,所以如果我犯了愚蠢的错误,请原谅。

采纳答案by John Kugelman

In order of preference:

按优先顺序:

# Good
if not list3:

# Okay
if len(list3) == 0:

# Ugly
if list3 == []:

# Silly
try:
    next(iter(list3))
    # list has elements
except StopIteration:
    # list is empty

If you have both an if and an else you might also re-order the cases:

如果您同时拥有 if 和 else,您还可以重新排序这些案例:

if list3:
    # list has elements
else:
    # list is empty

回答by Ali Afshar

Check its length.

检查其长度。

l = []
print len(l) == 0

回答by dawg

You find out if a list is empty by testing the 'truth' of it:

您可以通过测试列表的“真相”来确定列表是否为空:

>>> bool([])
False
>>> bool([0])     
True

While in the second case 0is False, but the list [0]is True because it contains something. (If you want to test a list for containing all falsey things, use allor any: any(e for e in li)is True if any item in liis truthy.)

虽然在第二种情况下0是 False,但列表[0]是 True,因为它包含一些东西。(如果要测试包含所有虚假内容的列表,请使用allany: any(e for e in li)is True 如果其中的任何项目li为真。)

This results in this idiom:

这导致了这个习语:

if li:
    # li has something in it
else:
    # optional else -- li does not have something 

if not li:
    # react to li being empty
# optional else...

According to PEP 8, this is the proper way:

根据PEP 8,这是正确的方法:

? 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)

? 对于序列(字符串、列表、元组),使用空序列为假的事实。

Yes: if not seq:
     if seq:

No: if len(seq)
    if not len(seq)

You test if a list has a specific index existing by using try:

您可以使用以下命令测试列表是否存在特定索引try

>>> try:
...    li[3]=6
... except IndexError:
...    print 'no bueno'
... 
no bueno

So you may want to reverse the order of your code to this:

因此,您可能希望将代码的顺序颠倒为:

if list3:  
    print list3  
else:  
    print "No matches found"

回答by Thanakron Tandavas

this is how you would do that

这就是你会怎么做

if len(list3) == 0:
    print("No matches found")  

回答by Kartik

Python provides an inbuilt any() function to check whether an iterable is empty or not:

Python 提供了一个内置的 any() 函数来检查可迭代对象是否为空:

>>>list=[]

>>>any(list)

False

The function returns True if the iterable contains a 'True' value, and False otherwise.

如果可迭代对象包含“真”值,则该函数返回 True,否则返回 False。

However, note that the list [0] also returns False with any().

但是,请注意列表 [0] 也使用 any() 返回 False。