Python 在一行中写入列表项

Python 在一行中写入列表项,python,Python,给定一个列表(可能不止一个),我想将列表中的项目写在一行中,用空格隔开。最好的方法是什么 比如说, 输入: sentence1=["I", "like", "yellow", "flowers"] sentence2=["I", "like", "blue", "sky"] 输出: I like yellow flowers I like blue sky 给你:) join()是一个字符串方法,它返回一个与iterable元素连接的字符串。语法:string.join(iterable)

给定一个列表(可能不止一个),我想将列表中的项目写在一行中,用空格隔开。最好的方法是什么

比如说,

输入:

sentence1=["I", "like", "yellow", "flowers"]
sentence2=["I", "like", "blue", "sky"]
输出:

I like yellow flowers
I like blue sky
给你:)

join()是一个字符串方法,它返回一个与iterable元素连接的字符串。语法:
string.join(iterable)


这将输出句子,每个句子在各自的行上

 print(" ".join(sentence1))

 print(" ".join(sentence2))

不过,请确保修复列表定义中的语法错误。

使用
*

sentence1=["I", "like", "yellow", "flowers"]
sentence2=["I", "like", "blue", "sky"]
print(*sentence1)
print(*sentence2)

此功能将帮助您:

    def join(*args):
            result = list()
            for i in args:
                result.extend(i)
            return ' '.join(result)
sentence1=["I", "like", "yellow", "flowers"]
sentence2=["I", "like", "blue", "sky"]
sentence3=["I", "like", "green", "grass"]
join(sentence1, sentence2, sentence3)

在Python3中,您可以使用

print(*mylist)
在Python 2中


使用空格连接每个列表?然后使用空格连接每个结果字符串<代码>str.join()。此外。。你在输入中遗漏了一个
”。
”。加入(句子1)
?那些反对票是为了什么?@cerebrou我没有否决你的问题,但我可以猜人们否决了你的问题,因为它非常简单,在谷歌搜索中很容易找到
print(*mylist)
for i in range(len(mylist)):
         print mylist[i],