在Python中以一行而不是字符打印每个单词?
考虑以下代码和输出 代码1: 产出1: 代码2: 输出2: 我打算打印如下 一个 是 一个 两个 是 两个 三 是 三在Python中以一行而不是字符打印每个单词?,python,Python,考虑以下代码和输出 代码1: 产出1: 代码2: 输出2: 我打算打印如下 一个 是 一个 两个 是 两个 三 是 三 我需要在哪里修改以按所需顺序打印?根据taoufik的评论,您希望在遍历每个句子之前对其进行修改。这是因为,在默认情况下,当您在Python中迭代字符串时,它将逐个字符进行迭代。通过调用split,可以按空格将字符串拆分为一个单词列表。见下文 ls = ["one is one","two is two","three is three"] for sentence in l
我需要在哪里修改以按所需顺序打印?根据taoufik的评论,您希望在遍历每个句子之前对其进行修改。这是因为,在默认情况下,当您在Python中迭代字符串时,它将逐个字符进行迭代。通过调用split,可以按空格将字符串拆分为一个单词列表。见下文
ls = ["one is one","two is two","three is three"]
for sentence in ls:
for word in sentence.split():
print(word)
one
is
one
two
is
two
three
is
three
试试这个:
ls = ["one is one","two is two","three is three"]
words = []
for each_item in ls:
words = each_item.split()
for word in words:
print(word)
对每个\u项目中的每个\u单词使用拆分方法。拆分为什么要向下投票?这是一个不符合标准的问题吗?虽然这个问题比较基本,但它的表述和提出都很好。我不认为向下投票是有理由的@hanugmNot向下投票人,但我认为他们是因为在输出中使用图像而不是实际文本。但无论如何,这是没有道理的。
ls = ["one is one","two is two","three is three"]
for sentence in ls:
for word in sentence.split():
print(word)
one
is
one
two
is
two
three
is
three
ls = ["one is one","two is two","three is three"]
words = []
for each_item in ls:
words = each_item.split()
for word in words:
print(word)