I need a condition which is satisfied if there are no '', in the
string.
您可以使用all来检查这个
In [1]: s1 = ['11-10-2017', '12:15 PM', 'B.ARTS', 'Linguistics', '', '', 'German', 'Name']
In [2]: s2 = ['11-10-2017', '12:15 PM']
In [4]: all(x for x in s1)
Out[4]: False
In [5]: all(x for x in s2)
Out[5]: True
您可以使用
all
来检查这个只需将^{} 与列表一起用作参数:
如果有任何元素是真的(即非空),它将返回
True
似乎要从列表中筛选空字符串:
条件是
if item
。澄清一下,''
是一个空字符串。在迭代过程中,如果item
是''
,则条件是False
,因此从结果中排除该项。否则,条件是True
,结果被添加到列表中。另见this post此行为是因为all objects in Python have "truthiness"-所有对象都假定为
True
,只有少数对象除外,例如False
、0
、""
、None
和空集合相关问题 更多 >
编程相关推荐