从python列表中获取第一个非空字符串 [英] Get first non-empty string from a list in python

查看:929
本文介绍了从python列表中获取第一个非空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,我有一个字符串列表,其中一些可能是空字符串.获取第一个非空字符串的最佳方法是什么?

In Python I have a list of strings, some of which may be the empty string. What's the best way to get the first non-empty string?

推荐答案

next(s for s in list_of_string if s)

Stephan202在评论中建议的py3k证明版本,谢谢.

py3k proof version as advised by Stephan202 in comments, thanks.

这篇关于从python列表中获取第一个非空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆