簡體   English   中英

如何在python的列表中放置新的行元素

[英]How to put new line elements in a list in python

我有這樣的輸入:li

There
Which
Who
Where
Why

現在我希望將這些單詞包含在列表中。 期望輸出

l = ['There', 'Which', 'Who', 'Where', 'Why']

我應該如何在 python 中實現它?

這是我當前的代碼

out=[]
buff=[]

for c in li:
    if c == '\n':
        out.append(''.join(buff))
        buff = []
    else:
        buff.append(c)
else:
    if buff:
        out.append(''.join(buff))
print out

但我得到的輸出是

[u'There']
[u'Which']
[u'Who']
[u'Where']
[u'Why']

而不是我想要的

嘗試使用它,它應該打印在一行而不是多行上。

array = ['Who', 'What', 'Where']

print(" ".join(map(str,array)))

暫無
暫無

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

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