如何在sh中去掉逗号和括号

2024-03-29 05:38:56 发布

您现在位置:Python中文网/ 问答频道 /正文

好的,在搜索了很多之后,我决定问一个问题,我试过打印[0],但是我得到了错误

Traceback (most recent call last):

文件“”,第1行,在 打印[0] TypeError:“内置函数或方法”对象不可下标

 ['a', 'c', 'e']
[1, 3, 5]

这是我的两个输出我想删除逗号和倒逗号,使它看起来像

ace
135

Tags: 文件对象方法函数most错误call内置
3条回答
''.join(a)
print ''.join(a)

对于整数列表[1,3,5],首先使用以下命令将元素更改为字符串:

map(lambda x: str(x), [1,3,5])

然后发布与第一行相同的行。如果你愿意,你可以改回int。你知道吗

要从列表的元素创建字符串,请执行以下操作:

my_list_1 = ['a', 'c', 'e']
my_list_2 = [1, 3, 5]

# The "join" works for both when list's element type is "string" or "int"
list_1_str = ''.join(map(str, my_list_1))
# output: "ace"

list_2_str = ''.join(map(str, my_list_2))
# output: "135"

对于带有str的列表:

x = ['a','c','e']
str1 = ''.join(x)

对于带有int的列表:

x = [1,2,3]
str1=''.join(str(y) for y in x)

这应该对你有用。你知道吗

相关问题 更多 >