简体   繁体   English

如何从此元组中删除引号? 我怎样才能使列表同质化

[英]How can i remove the quotation marks from this tuple? and how can i homogeneous the list

eg i have:例如我有:

"('orange', 'apple','banana')"

i got this as a result of:我得到这个是因为:

str(tuple(fruit_list))

how can remove the outer quotation marks?怎么去掉外引号?

EDIT:编辑:

in my list i have eg: list1 = ['orange',"fruit",'banana']在我的列表中,例如:list1 = ['orange',"fruit",'banana']

you can see some has double quotation marks, how can i replace it with '.你可以看到有些有双引号,我怎么能用 '. I have tried:我努力了:

[i.replace('"', '') for i in list1 ] 

but doesn't work但不起作用

You cannot delete those quotation marks as they are part of the string representation itself.您不能删除这些引号,因为它们是字符串表示本身的一部分。

If you want to display the result then print() it.如果要显示结果,请print()它。

This should do it:这应该这样做:

print(', '.join(result))

This creates a comma-delimited string of each list entry:这将为每个列表条目创建一个逗号分隔的字符串:

orange, apple, banana

Because they are parts of string representation, you can't.因为它们是字符串表示的一部分,所以你不能。 When you print(fruitlist) , this automatically works当您print(fruitlist)时,这会自动起作用

EDIT: Just saw that the comments say the same, but I wasn't looking at the comments while writing the post.编辑:刚刚看到评论说的一样,但我在写这篇文章时没有看评论。

the "('orange', 'apple','banana')" isnt a tuple, its a String you need to checkout the function/method that are returning that string; "('orange', 'apple','banana')" 不是元组,它是一个字符串,您需要检查返回该字符串的函数/方法;

If you cant modify the method/function that returns this String you need to create a function do disassemble the String and create a tuple from String content.如果你不能修改返回这个字符串的方法/函数,你需要创建一个 function 做反汇编字符串并从字符串内容创建一个元组。

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

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