Ton*_*yGW 3 python string list
假设我有一个列表['', 'Tom', 'Jane', 'John', '0'],我写了以下内容来检查它是否有空字符串'':
if any('' in s for s in row[1:]):
print "contains empty string, continue now"
continue
Run Code Online (Sandbox Code Playgroud)
我希望这只能选择空字符串,但这也会选择一个包含'0'的列表.'0'字符串有效,我不想将其过滤掉.那么如何在python列表中检查空字符串呢?
您可以检查'' 列表中是否有元素:
if '' in lst:
# ...
if '' in lst[:1]: # for your example
# ...
Run Code Online (Sandbox Code Playgroud)
这是一个例子:
>>> lst = ['John', '', 'Foo', '0']
>>> lst2 = ['John', 'Bar', '0']
>>> '' in lst
True
>>> '' in lst2
False
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13593 次 |
| 最近记录: |