簡體   English   中英

如何在shell中去除逗號,括號

[英]How do I strip commas, brackets in shell

好吧,經過大量搜索,我決定問我試過打印 [0] 的問題,但出現錯誤

Traceback (most recent call last):

文件 "", line 1, in print[0] TypeError: 'builtin_function_or_method' 對象不可下標

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

這些是我的 2 個輸出,我想刪除逗號和引號並使其看起來像

ace
135

對於帶有str列表:

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

對於帶有int列表:

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

這應該對你有用。

''.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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM