拆分和替换2D列表中的字符串

时间:2018-10-22 06:58:44

标签: python list nested-lists

我有一组数据,如下所示:

[['05-Feb-2001 12:00:01','A','<>TG:MIN MAX W1 GRN RED'],
 ['05-Feb-2001 12:00:01','B','MIN MAX'],
 ['05-Feb-2001 12:00:07','A','<i>TG:MAX MIN W2'],
 ['05-Feb-2001 12:00:07','C','MAX RED GRN'],
 ['05-Feb-2001 12:00:20','A','MIN MAX RED'],
 ['05-Feb-2001 12:01:00','A','<i>TG:MAX MIN RED GRN']]

如该行的3值所示,它可以包含或可以不包含“ <> TG”。我想检测

"<i>"

并将其从字符串中分离出来,然后添加为第4列

所需的输出将是

[['05-Feb-2001 12:00:01', 'A', '<>TG:MIN MAX W1 GRN RED'],
 ['05-Feb-2001 12:00:01', 'B', 'MIN MAX'],
 ['05-Feb-2001 12:00:07', 'A', 'TG:MAX MIN W2', '<i>'],
 ['05-Feb-2001 12:00:07', 'C', 'MAX RED GRN'],
 ['05-Feb-2001 12:00:20', 'A', 'MIN MAX RED'],
 ['05-Feb-2001 12:01:00', 'A', 'TG:MAX MIN RED GRN', '<i>']]

请咨询!

3 个答案:

答案 0 :(得分:2)

这对您有用吗?只需遍历子列表并就地更新即可。

for l in lst:
    if '<i>TG' in l[-1]:
        l[-1] = l[-1].replace('<i>', '')
        l.append('<i>')       

print(lst)

[['05-Feb-2001 12:00:01', 'A', '<>TG:MIN MAX W1 GRN RED'],
 ['05-Feb-2001 12:00:01', 'B', 'MIN MAX'],
 ['05-Feb-2001 12:00:07', 'A', 'TG:MAX MIN W2', '<i>'],
 ['05-Feb-2001 12:00:07', 'C', 'MAX RED GRN'],
 ['05-Feb-2001 12:00:20', 'A', 'MIN MAX RED'],
 ['05-Feb-2001 12:01:00', 'A', 'TG:MAX MIN RED GRN', '<i>']]

如果要在字符串开头检查令牌,请更改

if '<i>TG' in l[-1]

if l[-1].startswith('<i>TG')

(您不需要正则表达式。)

答案 1 :(得分:0)

更短的时候,请使用list comprehension

print([[y if x!=2 else y.replace('<i>','') for x,y in enumerate(i)]+['<i>'] if '<i>' in i[-1] else i for i in lst])

或者如果想在开头:

print([[y if x!=2 else y.lstrip('<i>') for x,y in enumerate(i)]+['<i>'] if i[-1].startswith('<i>') else i for i in lst])\

两个输出:

[['05-Feb-2001 12:00:01', 'A', '<>TG:MIN MAX W1 GRN RED'],
 ['05-Feb-2001 12:00:01', 'B', 'MIN MAX'],
 ['05-Feb-2001 12:00:07', 'A', 'TG:MAX MIN W2', '<i>'],
 ['05-Feb-2001 12:00:07', 'C', 'MAX RED GRN'],
 ['05-Feb-2001 12:00:20', 'A', 'MIN MAX RED'],
 ['05-Feb-2001 12:01:00', 'A', 'TG:MAX MIN RED GRN', '<i>']]

答案 2 :(得分:0)

您可以编写一个函数来检查列表数据列表中的一个条目,然后map将其应用于列表:

data = [['05-Feb-2001 12:00:01','A','<>TG:MIN MAX W1 GRN RED'],
        ['05-Feb-2001 12:00:01','B','MIN MAX'],
        ['05-Feb-2001 12:00:07','A','<i>TG:MAX MIN W2'],
        ['05-Feb-2001 12:00:07','C','MAX RED GRN'],
        ['05-Feb-2001 12:00:20','A','MIN MAX RED'],
        ['05-Feb-2001 12:01:00','A','<i>TG:MAX MIN RED GRN']]

def separate(entry, separator='<i>', position=2):
    return [
        el.replace(separator, '') if idx==position else el for idx, el in enumerate(entry)] + (
            [] if not separator in entry[position] 
            else [separator]
        )

from itertools import repeat
map(separate, data, repeat('<i>'), repeat(2))

Out[26]: 
[['05-Feb-2001 12:00:01', 'A', '<>TG:MIN MAX W1 GRN RED'],
['05-Feb-2001 12:00:01', 'B', 'MIN MAX'],
['05-Feb-2001 12:00:07', 'A', 'TG:MAX MIN W2', '<i>'],
['05-Feb-2001 12:00:07', 'C', 'MAX RED GRN'],
['05-Feb-2001 12:00:20', 'A', 'MIN MAX RED'],
['05-Feb-2001 12:01:00', 'A', 'TG:MAX MIN RED GRN', '<i>']]

注意:如果您使用的是python 2.x,则repeat需要附加参数times=len(data)