如何打印列表中字符串中的单词?在 Python
How to print a word thats in a string thats in a list? in Python
我是 python 的新手,非常感谢您的帮助。
这实际上是一个更大函数的一部分,但我基本上是在尝试从列表中的字符串中调用一个单词。
这是我想出的一个例子:
字数 = ['i am sam', 'sam i am', 'green eggs and ham']
for x in words:
for y in x:
print(y)
这将打印每个字符:
i
a
m
s
a
m
s
a
m
i
a
m... etc.
但我想要每个字(空格无关紧要):
i
am
sam
sam
i
am....etc.
你有一个额外的 for 循环。
for x in words:
print x
这是输出:
我是山姆
山姆我是
绿色鸡蛋和火腿
x 是数组中的每个字符串。
希望我对您的 post 理解正确,您想要打印数组中的每个单词。
您可以使用 for each 循环,然后使用 split 打印其中的每个单词。
for string in words:
wordArray = string.split(" ")
for word in wordArray:
print word
split 会将您的字符串变成一个数组,每个元素由传递给 split 的参数分隔(在本例中为 space)
Try this:
for x in words:
for y in x.split(' '):
print y
你需要做的是当你得到字符串 "i am sam" 然后用 Space 拆分这个字符串并将其存储在其他数组中然后在新数组上应用其他循环作为
sentence = ['i am sam', 'sam i am', 'green eggs and ham']
for x in sentence:('\n')
print x
words = x.split(" ")
for y in words:
print(y)
现在在这里
words = x.split(" ")
因为你已经拆分了句子 x 你会得到 words=['i','am','sam']
进一步你可以检查Python regex separate space-delimited words into a list
还有这个 How to split a string into a list?
我认为您正在寻找 split()
函数:
phrases = ['i am sam', 'sam i am', 'green eggs and ham']
for x in phrases:
words = x.split()
for y in words:
print(y)
这将为您将每个短语拆分成单词。
您需要致电 split
:
for element in words:
for word in element.split(' '):
print word
words = ['i am sam ', 'sam i am ', ' green eggs and ham']
for string in words:
for str in string.split():
print(str)
print()
我试着在你的话里加一个以上space
顺便说一句,这是我的第一个 python 程序,谢谢你:)
解决方法如下:
for i in words:
print i
k=i.split(' ')
print k
i am sam
['i', 'am', 'sam']
sam i am
['sam', 'i', 'am']
green eggs and ham
['green', 'eggs', 'and', 'ham']
如果您需要对已打印的单词执行任何其他操作,这种方法很有用,因为它会在打印前将它们存储在列表中:
z = (' '.join(words)).split()
for x in z:
print x
第一行轮流列表words = ['i am sam', 'sam i am', 'green eggs and ham']
成 z = ['i', 'am', 'sam', 'sam', 'i', 'am', 'green' , 'eggs', 'and', 'ham']
for 循环只是遍历此列表并一次打印出一项。
如果你愿意,你可以做到
单词 = (' '.join(单词)).split()
如果你想覆盖旧列表
我是 python 的新手,非常感谢您的帮助。 这实际上是一个更大函数的一部分,但我基本上是在尝试从列表中的字符串中调用一个单词。 这是我想出的一个例子:
字数 = ['i am sam', 'sam i am', 'green eggs and ham']
for x in words:
for y in x:
print(y)
这将打印每个字符:
i
a
m
s
a
m
s
a
m
i
a
m... etc.
但我想要每个字(空格无关紧要):
i
am
sam
sam
i
am....etc.
你有一个额外的 for 循环。
for x in words:
print x
这是输出: 我是山姆 山姆我是 绿色鸡蛋和火腿
x 是数组中的每个字符串。
希望我对您的 post 理解正确,您想要打印数组中的每个单词。
您可以使用 for each 循环,然后使用 split 打印其中的每个单词。
for string in words:
wordArray = string.split(" ")
for word in wordArray:
print word
split 会将您的字符串变成一个数组,每个元素由传递给 split 的参数分隔(在本例中为 space)
Try this: for x in words: for y in x.split(' '): print y
你需要做的是当你得到字符串 "i am sam" 然后用 Space 拆分这个字符串并将其存储在其他数组中然后在新数组上应用其他循环作为
sentence = ['i am sam', 'sam i am', 'green eggs and ham']
for x in sentence:('\n')
print x
words = x.split(" ")
for y in words:
print(y)
现在在这里
words = x.split(" ")
因为你已经拆分了句子 x 你会得到 words=['i','am','sam']
进一步你可以检查Python regex separate space-delimited words into a list 还有这个 How to split a string into a list?
我认为您正在寻找 split()
函数:
phrases = ['i am sam', 'sam i am', 'green eggs and ham']
for x in phrases:
words = x.split()
for y in words:
print(y)
这将为您将每个短语拆分成单词。
您需要致电 split
:
for element in words:
for word in element.split(' '):
print word
words = ['i am sam ', 'sam i am ', ' green eggs and ham']
for string in words:
for str in string.split():
print(str)
print()
我试着在你的话里加一个以上space
顺便说一句,这是我的第一个 python 程序,谢谢你:)
解决方法如下:
for i in words:
print i
k=i.split(' ')
print k
i am sam
['i', 'am', 'sam']
sam i am
['sam', 'i', 'am']
green eggs and ham
['green', 'eggs', 'and', 'ham']
如果您需要对已打印的单词执行任何其他操作,这种方法很有用,因为它会在打印前将它们存储在列表中:
z = (' '.join(words)).split()
for x in z:
print x
第一行轮流列表words = ['i am sam', 'sam i am', 'green eggs and ham']
成 z = ['i', 'am', 'sam', 'sam', 'i', 'am', 'green' , 'eggs', 'and', 'ham']
for 循环只是遍历此列表并一次打印出一项。
如果你愿意,你可以做到 单词 = (' '.join(单词)).split() 如果你想覆盖旧列表