如何使用 python 从列表中删除方括号和单引号?
How to remove square brackts and single quotes from a list using python?
我有一个如下所示的列表
list_val = ['1','2','3','4']
我想从列表中删除方括号和单引号。我喜欢得到如下所示的输出
list_new = 1,2,3,4
可能吗?期待快速帮助。提前致谢。
简单
list_new = [int(x) for x in list_val]
对于输出,不要使用 Python repr 表达。在这里,使用 join:
list_val = ['1','2','3','4']
print 'list_new = %s' % ','.join(list_val)
list_new = 1,2,3,4
这个表达式等价于tuple assignment
。
>>> list_val = ['1','2','3','4']
>>> list_new = tuple(map(lambda x:int(x), list_val))
>>> list_new
(1, 2, 3, 4)
相当于:
>>> list_new = 1, 2, 3, 4
>>> list_new
(1, 2, 3, 4)
我有一个如下所示的列表
list_val = ['1','2','3','4']
我想从列表中删除方括号和单引号。我喜欢得到如下所示的输出
list_new = 1,2,3,4
可能吗?期待快速帮助。提前致谢。
简单
list_new = [int(x) for x in list_val]
对于输出,不要使用 Python repr 表达。在这里,使用 join:
list_val = ['1','2','3','4']
print 'list_new = %s' % ','.join(list_val)
list_new = 1,2,3,4
这个表达式等价于tuple assignment
。
>>> list_val = ['1','2','3','4']
>>> list_new = tuple(map(lambda x:int(x), list_val))
>>> list_new
(1, 2, 3, 4)
相当于:
>>> list_new = 1, 2, 3, 4
>>> list_new
(1, 2, 3, 4)