![](/img/trans.png)
[英]how to separate different elements in a list in python and put them in a new list
[英]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.