我试图在列表中使用("")空对象来分割我的Python列表。
['', u'WO0000008971346', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971321', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971307', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971247', u'', u'Low', u'Pending', u'Client Action Required',
u'17/04/2018 15:08:49','', u'WO0000008971245',u'', u'Low', u'Pending', u'Client Action Required',
u'17/04/2018 15:07:10','', u'WO0000008971235', u'',
u'Low', u'In Progress', u'', u'17/04/2018 15:03:50']
使用python拆分它的任何约定?
答案 0 :(得分:4)
你可能的意思是“从列表中创建子列表,用空字符串分隔”。
在这种情况下,使用itertools.groupby
,条件为“字符串为空”:
import itertools
s = ['', u'WO0000008971346', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971321', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971307', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971247', u'', u'Low', u'Pending', u'Client Action Required', u'17/04/2018 15:08:49',
'', u'WO0000008971245',u'', u'Low', u'Pending', u'Client Action Required', u'17/04/2018 15:07:10',
'', u'WO0000008971235', u'', u'Low', u'In Progress', u'', u'17/04/2018 15:03:50']
result = [list(x) for k,x in itertools.groupby(s,key=bool) if k]
print(result)
bool
是关键函数,如果字符串不为空,则生成True
。然后我们过滤True
条件以保留非空组。
结果:
[['WO0000008971346'], ['Low', 'Assigned'], ['WO0000008971321'], ['Low', 'Assigned'],
['WO0000008971307'], ['Low', 'Assigned'], ['WO0000008971247'],
['Low', 'Pending', 'Client Action Required', '17/04/2018 15:08:49'],
['WO0000008971245'], ['Low', 'Pending', 'Client Action Required', '17/04/2018 15:07:10'],
['WO0000008971235'], ['Low', 'In Progress'], ['17/04/2018 15:03:50']]
如果你想要删除多次出现的空字符串(保留一个扁平列表,用空字符串分隔,这是相同的想法,但有一个展平,有条件:
result2 = list(itertools.chain.from_iterable(x if k else [''] for k,x in itertools.groupby(s,key=bool)))
的产率:
['', 'WO0000008971346', '', 'Low', 'Assigned', '', 'WO0000008971321', '',
'Low', 'Assigned', '', 'WO0000008971307', '', 'Low', 'Assigned', '',
'WO0000008971247', '', 'Low', 'Pending', 'Client Action Required',
'17/04/2018 15:08:49', '', 'WO0000008971245', '', 'Low', 'Pending', 'Client Action Required',
'17/04/2018 15:07:10', '', 'WO0000008971235', '', 'Low', 'In Progress', '', '17/04/2018 15:03:50']
答案 1 :(得分:0)
如果我理解正确,删除空元素后需要3个元素的子列表。
<强>演示强>
data = ['', u'WO0000008971346', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971321', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971307', u'', u'Low', u'Assigned', u'', u'',
'', u'WO0000008971247', u'', u'Low', u'Pending', u'Client Action Required', u'17/04/2018 15:08:49',
'', u'WO0000008971245',u'', u'Low', u'Pending', u'Client Action Required', u'17/04/2018 15:07:10',
'', u'WO0000008971235', u'', u'Low', u'In Progress', u'', u'17/04/2018 15:03:50']
data = filter(None, data)
print([data[x:x+3] for x in xrange(0, len(data), 3)])
<强>输出:强>
[[u'WO0000008971346', u'Low', u'Assigned'], [u'WO0000008971321', u'Low', u'Assigned'], [u'WO0000008971307', u'Low', u'Assigned'], [u'WO0000008971247', u'Low', u'Pending'], [u'Client Action Required', u'17/04/2018 15:08:49', u'WO0000008971245'], [u'Low', u'Pending', u'Client Action Required'], [u'17/04/2018 15:07:10', u'WO0000008971235', u'Low'], [u'In Progress', u'17/04/2018 15:03:50']]