如何删除嵌套列表内的字符串的引号

时间:2018-12-12 22:43:05

标签: python python-3.x

我有一个代码块,如下所示:

lst=[67310,0,"May the force be with you"]
print(" ".join(repr(i) for i in lst).replace("'",""))

输出为:

67310 0 May the force be with you

但是,如果我有一个列表,像这样:

lst=[[67310,0,"May the force be with you"],[65310,1,"I'm getting too old for this stuff"]]
for j in lst:
    print(" ".join(repr(i) for i in j).replace("'",""))

输出为:

67310 0 May the force be with you
65310 1 "Im getting too old for this stuff"

问题是我想要一个不带引号的输出:

67310 0 May the force be with you
65310 1 I'm getting too old for this stuff

如何轻松解决此问题?感谢您的帮助

2 个答案:

答案 0 :(得分:3)

只需尝试一下,我想这就是您想要的。

lst=[[67310,0,"May the force be with you"],[65310,1,"I'm getting too old for this stuff"]]
for  j in lst:
    print(" ".join(str(i) for i in j).replace("'",""))

# 67310 0 May the force be with you
# 65310 1 Im getting too old for this stuff

答案 1 :(得分:0)

尝试以下方法:

for j in lst:
    print(" ".join(repr(i) for i in j).replace('"', ''))

在您的.replace中,您要替换的是',而不是"