相关疑难解决方法(0)

替换列表列表中的字符串

我有一个字符串列表列表,如:

example = [["string 1", "a\r\ntest string:"],["string 1", "test 2: another\r\ntest string"]]
Run Code Online (Sandbox Code Playgroud)

我想"\r\n"用空格替换(并删除":"所有字符串的末尾).

对于普通列表,我会使用列表理解来删除或替换项目

example = [x.replace('\r\n','') for x in example]
Run Code Online (Sandbox Code Playgroud)

甚至是lambda函数

map(lambda x: str.replace(x, '\r\n', ''),example)
Run Code Online (Sandbox Code Playgroud)

但我不能让它为嵌套列表工作.有什么建议?

python replace list nested-lists python-3.x

8
推荐指数
2
解决办法
8534
查看次数

标签 统计

list ×1

nested-lists ×1

python ×1

python-3.x ×1

replace ×1