Phi*_*l H 2 python iterator python-itertools
我有一个列表,其中包含许多内容:
lista = ['a', 'b', 'foo', 'c', 'd', 'e', 'bar']
Run Code Online (Sandbox Code Playgroud)
我想获得列表中第一个满足谓词的项目len(item) > 2.有没有一种更简洁的方法来做到这一点而不是itertools的dropwhile和next?
first = next(itertools.dropwhile(lambda x: len(x) <= 2, lista))
Run Code Online (Sandbox Code Playgroud)
我最初使用[item for item in lista if len(item)>2][0]过,但这需要python首先生成整个列表.
>>> lista = ['a', 'b', 'foo', 'c', 'd', 'e', 'bar']
>>> next(i for i in lista if len(i) > 2)
'foo'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
206 次 |
| 最近记录: |