删除子列表中的字符
Delete character in sublist
我有一个子列表列表,格式如下:
[['1023869', '-0.137869328454', '0.318039373746', '1.243\n'], ['1023870', '-0.121434633793', '0.324670024328', '1.243\n']]
我想从子列表的最后一个元素中删除“\n”。
使用列表理解 rstrip
:
[sublist[:-1] + [sublist[-1].rstrip()] for sublist in lst]
[['1023869', '-0.137869328454', '0.318039373746', '1.243'],
['1023870', '-0.121434633793', '0.324670024328', '1.243']]
只是给定列表中可变的最后一个元素。
for subList in alist:
subList[-1] = subList[-1][:-1]
>>> alist
>>> [['1023869', '-0.137869328454', '0.318039373746', '1.243'], ['1023870', '-0.121434633793', '0.324670024328', '1.243']]
我有一个子列表列表,格式如下:
[['1023869', '-0.137869328454', '0.318039373746', '1.243\n'], ['1023870', '-0.121434633793', '0.324670024328', '1.243\n']]
我想从子列表的最后一个元素中删除“\n”。
使用列表理解 rstrip
:
[sublist[:-1] + [sublist[-1].rstrip()] for sublist in lst]
[['1023869', '-0.137869328454', '0.318039373746', '1.243'],
['1023870', '-0.121434633793', '0.324670024328', '1.243']]
只是给定列表中可变的最后一个元素。
for subList in alist:
subList[-1] = subList[-1][:-1]
>>> alist
>>> [['1023869', '-0.137869328454', '0.318039373746', '1.243'], ['1023870', '-0.121434633793', '0.324670024328', '1.243']]