简体   繁体   English

如何从 python 的列表中删除撇号?

[英]How to remove apostrophes from a list in python?

I need to remove apostrophes -> ' <- from a list, within python, without using any add-ons, so only built in functions.我需要在 python 中从列表中删除撇号 -> ' <- ,而不使用任何附加组件,因此只使用内置函数。

Eg I need a list like:例如,我需要一个类似的列表:

lista = ['a', 'b', 'c', 'd']

into进入

lista = [ a, b, c, d]

I've tried using for with.replace or making the list into a string then replacing, but I haven't had anything work yet.我试过使用 for with.replace 或将列表变成一个字符串然后替换,但我还没有任何工作。

Any chance you could help?你有什么可以帮忙的机会吗?

You cannot remove apostrophes from a list because that's pretty much how you identify what a list is.您不能从列表中删除撇号,因为这几乎就是您识别列表是什么的方式。 However the apostrophes will not interfere with anything you do with the list.但是,撇号不会干扰您对列表所做的任何事情。 The apostrophes show that the elements are strings.撇号表明元素是字符串。

You can use str.join() to concatenate strings with a specified separator.您可以使用str.join()将字符串与指定的分隔符连接起来。

For example:例如:

>>> strings = ['A', 'B', 'C']
>>> print(', '.join(strings))
A, B, C

Furthermore, in your case, str.format() may also help:此外,在您的情况下, str.format()也可能有所帮助:

>>> strings = ['A', 'B', 'C']
>>> print('strings = [{}]'.format(', '.join(strings)))
strings = [A, B, C]

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

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