use*_*415 4 python split element list
作为这个问题的后续内容:在python中拆分列表元素
给出清单:
l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847', '']
Run Code Online (Sandbox Code Playgroud)
我怎样才能得到一切\t?
我试过了:
>>> [i.split('\t', 1)[1] for i in t]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
是因为我''到底了吗?我如何排除它?
In [175]: l = ['element1\t0238.94', 'element2\t2.3904', 'element3\t0139847', '']
In [176]: [i.partition('\t')[-1] for i in l]
Out[176]: ['0238.94', '2.3904', '0139847', '']
Run Code Online (Sandbox Code Playgroud)
或者,如果您只想考虑带有a的元素'\t':
In [177]: [i.partition('\t')[-1] for i in l if '\t' in i]
Out[177]: ['0238.94', '2.3904', '0139847']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
782 次 |
| 最近记录: |