从python列表项中删除单引号 [英] Remove single quotes from python list item
本文介绍了从python列表项中删除单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实际上很简单的问题: 我有一个像这样的python列表:
Actually quite simple question: I've a python list like:
['1','2','3','4']
只是想知道如何删除那些单引号?
我要[1,2,3,4]
Just wondering how can I strip those single quotes?
I want [1,2,3,4]
推荐答案
当前列表中的所有值都是字符串,并且您希望它们为整数,这是最简单的两种方法:
Currently all of the values in your list are strings, and you want them to integers, here are the two most straightforward ways to do this:
map(int, your_list)
和
[int(value) for value in your_list]
请参阅 map()和
See the documentation on map() and list comprehensions for more info.
如果您希望将列表中的项目保留为字符串,但不使用单引号将其显示,则可以使用以下内容:
If you want to leave the items in your list as strings but display them without the single quotes, you can use the following:
print('[' + ', '.join(your_list) + ']')
这篇关于从python列表项中删除单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文