我在代码的循环中使用下面的一行

importer = exporterslist.pop(0)

如果exporterslist没有条目或它是null,它将返回error: IndexError: pop from empty list。如何绕过没有条目的exporterslist?
我能想到的一个想法是,如果exporterslist不为空,则importer = exporterslist.pop(0)
否则获取循环中的下一个条目。
如果这个想法是正确的,如何用python编写代码呢?

最佳答案

你走对了。

if exporterslist: #if empty_list will evaluate as false.
    importer = exporterslist.pop(0)
else:
    #Get next entry? Do something else?

关于python - Python:从空列表中弹出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31216428/

10-12 18:34