简体   繁体   English

在列表列表中编辑字符串

[英]Editing strings in a list of lists

I've made a list of lists from a text block where each list contains all the words in a line as a separate element, like this: 我从一个文本块中制成了一个列表列表,其中每个列表包含一行中的所有单词作为单独的元素,如下所示:

listoflists = [['Lorem', 'ipsum', 'dolor', 'sit', 'amet\n']
               ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis\n']]

I want to be able to move through the list of lists, and remove the '\\n' from the last element of each list in the list of lists. 我希望能够在列表列表中移动,并从列表列表中的每个列表的最后一个元素中删除“ \\ n”。

This is the script I wrote to try to do this, but it doesn't work because when I return n, it only returns the first element of the first list. 这是我为尝试执行此操作而编写的脚本,但是它不起作用,因为当我返回n时,它仅返回第一个列表的第一个元素。

def remove_linebreaks(input):
for i in data:
    for n in i:
        if '\n' in n:
            n.strip('\n')
            return n
        else:
            return n
return input

Are there any other ways I could do this? 我还有其他方法可以做到这一点吗?

Why don't you just do return n.strip('\\n') ? 您为什么不只return n.strip('\\n') You can get rid of the if/else 您可以摆脱if / else

You could use a list comprehension 您可以使用列表理解

listoflists = [['Lorem', 'ipsum', 'dolor', 'sit', 'amet\n'],
               ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis\n']]

[[j.replace("\n","") for j in i] for i in listoflists]

Output 产量

[['Lorem', 'ipsum', 'dolor', 'sit', 'amet'],
 ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis']]

Just iterate over each list and replace the last element with the last element with the '\\n' stripped. 只需遍历每个列表,然后将最后一个元素替换为最后一个带有'\\n'元素的元素即可。

>>> listoflists = [['Lorem', 'ipsum', 'dolor', 'sit', 'amet\n'],
               ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis\n']]
>>> for elem in listoflists:
        elem[-1] = elem[-1].strip('\n')


>>> listoflists
[['Lorem', 'ipsum', 'dolor', 'sit', 'amet'], ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis']]

EDIT - Since in the code, you seem to be stripping \\n from each element you could do 编辑-由于在代码中,您似乎要从每个可以执行的元素中剥离\\n

>>> [[elem.strip('\n') for elem in lst] for lst in listoflists]
[['Lorem', 'ipsum', 'dolor', 'sit', 'amet'], ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis']]
listoflists = [['Lorem', 'ipsum', 'dolor', 'sit', 'amet\n'],['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis\n']]
for l in listoflists:
    l[-1] = l[-1].replace('\n','')
print listoflists

""" Output: “”“输出:

[['Lorem', 'ipsum', 'dolor', 'sit', 'amet'], ['consectetur', 'adipiscing', 'elit', 'donec', 'iaculis']] [['Lorem','ipsum','dolor','sit','amet'],['consectetur','adipiscing','elit','donec','iaculis']

""" “””

Also, the reason your original code does not work is b/c n.strip('\\n') does not change the value of n . 另外,您的原始代码不起作用的原因是b / c n.strip('\\n')不会更改n的值。 It returns a new string. 它返回一个新字符串。 If you did something like n=n.strip('\\n'); return n 如果您做了类似n=n.strip('\\n'); return n n=n.strip('\\n'); return n , that would work. n=n.strip('\\n'); return n ,那将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM