替换列表的多个列表中的一个元素

时间:2019-08-05 08:49:35

标签: python-3.x list replace

列表中有多个列表,我想替换每个列表中的“ \ xa0”,但我不知道该如何执行我的示例列表

[['0001/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'LSSZEC18033999', '\xa0'], 
['0001/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '40693008366', '\xa0'], 
['0002/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'APLU750808254', 'HTHC18032101'], 
['0002/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '02037823030', '\xa0'], 
['0003/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'LSSZEC18032365', '\xa0'], 
['0003/19-20', 'NHAVA SHEVA SEA (INNSA1)', 'SHAE19030155', '\xa0'], 
['0004/18-19', 'NHAVA SHEVA SEA (INNSA1)', '0258A33647', 'LLLNVS842311NVS'], 
['0004/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '17602776476', '\xa0'], 
['0005/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'APLU750808254', 'HTHC18032101'], 
['0005/19-20', 'NHAVA SHEVA SEA (INNSA1)', 'SNKO02A190301057', '\xa0'], 
['0006/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'SZWY18030109', '\xa0'], 
['0006/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '40684842450', '3986'], 
['0007/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'SRL18030520', '\xa0'], 
['0007/19-20', 'NHAVA SHEVA SEA (INNSA1)', 'HDMUJPNS1768154', '\xa0'], 
['0008/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'YSNBF18030315', '\xa0'], 
['0008/19-20', 'MUMBAI', 'CTLQD19036504', '\xa0'], ['0009/18-19', 'NHAVA 
SHEVA SEA (INNSA1)', 'SNKO02A180300433', '\xa0'], ['0009/19-20', 'SAHAR AIR 
CARGO ACC (INBOM4)', '51404381786', 'X8867ANKF7X'], ['0010/18-19', 'NHAVA 
SHEVA SEA (INNSA1)', 'SNKO02A180300587', '\xa0'], ['0010/19-20', 'NHAVA 
SHEVA SEA (INNSA1)', 'SRL19030377', '\xa0']]

需要帮助。

2 个答案:

答案 0 :(得分:1)

尝试以下代码,希望对您有所帮助。

data = [['0001/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'LSSZEC18033999', '\xa0'], ['0001/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '40693008366', '\xa0'], ['0002/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'APLU750808254', 'HTHC18032101'], ['0002/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '02037823030', '\xa0'], ['0003/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'LSSZEC18032365', '\xa0'], ['0003/19-20', 'NHAVA SHEVA SEA (INNSA1)', 'SHAE19030155', '\xa0'], ['0004/18-19', 'NHAVA SHEVA SEA (INNSA1)', '0258A33647', 'LLLNVS842311NVS'], ['0004/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '17602776476', '\xa0'], ['0005/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'APLU750808254', 'HTHC18032101'], ['0005/19-20', 'NHAVA SHEVA SEA (INNSA1)', 'SNKO02A190301057', '\xa0'], ['0006/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'SZWY18030109', '\xa0'], ['0006/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '40684842450', '3986'], ['0007/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'SRL18030520', '\xa0'], ['0007/19-20', 'NHAVA SHEVA SEA (INNSA1)', 'HDMUJPNS1768154', '\xa0'], ['0008/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'YSNBF18030315', '\xa0'], ['0008/19-20', 'MUMBAI', 'CTLQD19036504', '\xa0'], ['0009/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'SNKO02A180300433', '\xa0'], ['0009/19-20', 'SAHAR AIR CARGO ACC (INBOM4)', '51404381786', 'X8867ANKF7X'], ['0010/18-19', 'NHAVA SHEVA SEA (INNSA1)', 'SNKO02A180300587', '\xa0'], ['0010/19-20', 'NHAVA SHEVA SEA (INNSA1)', 'SRL19030377', '\xa0']]
newdata = [[sent.replace(u'\xa0', u' ') for sent in lst]for lst in data]

print(newdata)

答案 1 :(得分:0)

in_list = [['123', '\xa0'], ['123', '\xa0'], ['123', '\xa0'], ['123', '\xa0']]
out_list = [[i.replace('\xa0', '') if i == '\xa0' else i for i in sub_list] for sub_list in in_list]