简化卷积ifelse条件检查len(list)=0 Python

2024-06-26 14:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图检查sent1或sent2的长度是否为零,如果有,我想sent_witn_not_null设置为具有非零列表的列表。但我写的if-else条件似乎很复杂。什么是更简单的方法?在

sent1 = ["this","is","foo","bar"]
sent2 = []

if len(sent1) or len(sent2) == 0:
    sent_with_not_null = sent2 if len(sent1) == 0 else sent1
    sent_with_not_null = sent1 if len(sent2) == 0 else sent2

Tags: 方法列表lenifiswithnotthis