Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

Replace list brackets in Python 3.x

I have multiple list objects

Example:

data = [['id', 'host'], [1, '123']]

print(data)

Output:

[['id', 'host'], [1, '123']]

When:

print(*data)

Output:

['id', 'host'] [1, '123']

When:

print(*data, sep='
')

or

print('
'.join(map(str, data)))

Output:

['id', 'host']
[1, '123']

How to make an output like this without brackets and commas:

id host
1  123
question from:https://stackoverflow.com/questions/65910881/replace-list-brackets-in-python-3-x

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A straight-forward for loop gives your desired output:

data = [['id', 'host'], [1, '123']]
for d in data:
    print(*d)

If there's a problem with that approach, it's not apparent in your question.

If you're trying to produce a str result you can use a pair of join calls to convert the inner elements to str then join with spaces, then join those lines with ' '

data = [['id', 'host'], [1, '123']]
result = '
'.join(' '.join(str(e) for e in d) for d in data)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...