Sim*_*n D 10 python string list
在Python中,我有一个字符串列表,其中一些可能是空字符串.获得第一个非空字符串的最佳方法是什么?
Woj*_*ski 21
next(s for s in list_of_string if s)
编辑:由Stephan202在评论中建议的py3k校对版,谢谢.
要删除所有空字符串,
[s for s in list_of_strings if s]
要获取第一个非空字符串,只需创建此列表并获取第一个元素,或使用wuub建议的惰性方法.
| 归档时间: | 
 | 
| 查看次数: | 13154 次 | 
| 最近记录: |