如何停止输出在列表中的每个项目之后添加新行

问题描述 投票:0回答:1

电流输出:

从前有一只狗狂吠 吠叫 曾是 A 名字

所需输出:

从前有一只狗狂吠 吠叫是一个名字

代码:

list1 = ['There', 'once', 'was', 'a', 'dog', 'who', 'barked']
list2 = ['name']
print(' '.join(list1))
print(list1[-1]), print(list1[2]), print(list1[3]), print(list2[0])


# There once was a dog who barked
# barked was a dog name
# There once was a dog who barked
# name a dog barked once was

`


I tried using the join list function, but that did not work very well.  Looking for recommendations thank you
python list pycharm
1个回答
0
投票

肯定有一些重复的地方,但你需要事先连接列表:

list1 = ['There', 'once', 'was', 'a', 'dog', 'who', 'barked']
list2 = ['name']

output = " ".join(list1 + list2)
print(output)

哪个产量

There once was a dog who barked name
© www.soinside.com 2019 - 2024. All rights reserved.