我得到了一个路径列表,我需要以便携方式提取每个路径的第一个元素,我该怎么做?
['/abs/path/foo',
'rel/path',
'just-a-file']
Run Code Online (Sandbox Code Playgroud)
至
['abs', 'rel', 'just-a-file']
Run Code Online (Sandbox Code Playgroud)
在此先感谢Oli
In [69]: import os
In [70]: paths
Out[70]: ['/abs/path/foo', 'rel/path', 'just-a-file']
In [71]: [next(part for part in path.split(os.path.sep) if part) for path in paths]
Out[71]: ['abs', 'rel', 'just-a-file']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3554 次 |
| 最近记录: |