简体   繁体   English

如何在列表中的一个字符串中打印一个单词 在Python中

[英]How to print a word thats in a string thats in a list? in Python

I'm new to python and would greatly appreciate some help. 我是python的新手,不胜感激。 This is actually a part of a larger function but I am basically trying to call a word from a string that is in a list. 这实际上是较大函数的一部分,但我基本上是想从列表中的字符串中调用一个单词。 Here's an example I came up with: 这是我想出的一个例子:

words = ['i am sam', 'sam i am', 'green eggs and ham'] 单词= [“我是山姆”,“我是山姆”,“绿鸡蛋和火腿”]

for x in words:
    for y in x:
        print(y)

this prints every character: 这将打印每个字符:

i

a
m

s
a
m

s
a
m

i

a
m... etc.

but I want every word(the spaces do not matter): 但我想要每个字(空格并不重要):

i
am
sam

sam
i
am....etc.

I hope I am understanding your post correctly, you want to print every word in the array. 希望我能正确理解您的帖子,您希望打印数组中的每个单词。

You can use a for each loop and just print each word in it using split. 您可以为每个循环使用a,并仅使用split打印其中的每个单词。

for string in words:
    wordArray = string.split(" ")
    for word in wordArray:
        print word

split will turn your string into an array with each element seperated by the argument passed into split (in this case as space) split将把您的字符串变成一个数组,每个元素由传递给split的参数分隔(在本例中为空格)

Try this:

for x in words:
    for y in x.split(' '):
        print y

You will need to call split : 您将需要致电split

for element in words:
    for word in element.split(' '):
        print word

This way is useful if you ever need to do anything else with the words you've printed as it stores them in a List for you before printing: 如果您需要对已打印的单词进行任何其他操作,则此方法很有用,因为它将在打印之前将它们存储在“列表”中:

z = (' '.join(words)).split()
for x in z: 
    print x

The first line turns the list words = ['i am sam', 'sam i am', 'green eggs and ham'] 第一行将列表单词= ['i am sam','sam i am','green egg and ham']

into z = ['i', 'am', 'sam', 'sam', 'i', 'am', 'green', 'eggs', 'and', 'ham'] 变成z = ['i','am','sam','sam','i','am','green','eggs','and','ham']

The for loop just iterates through this list and prints out the items one at a time. for循环仅遍历此列表并一次打印出一个项目。

If you wanted you could do words = (' '.join(words)).split() if you wanted to overwrite the old list 如果您想覆盖旧列表,可以使用words =(''.join(words))。split()

You have an extra for loop in there. 那里有一个额外的for循环。

for x in words:
    print x

this is the output: i am sam sam i am green eggs and ham 这是输出:我是山姆,我是绿色鸡蛋和火腿

x is each string in the array. x是数组中的每个字符串。

What you have to do is while you get the String "i am sam" then Split this string by Space and store that in other array and then apply other loop on the new Array as 您要做的是,在获取字符串“ i am sam”后,按空格将该字符串拆分,并将其存储在其他数组中,然后在新数组上应用其他循环为

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)

now here 现在在这里

words = x.split(" ") as you have split the sentence x you will get as words=['i','am','sam'] words = x.split(" ")因为您将句子x拆分为words=['i','am','sam']

Further you can check Python regex separate space-delimited words into a list and this one How to split a string into a list? 此外,您可以检查Python regex将空格分隔的单词分隔成一个列表,而这一个如何将字符串拆分成一个列表?

I think you're looking for the split() function: 我认为您正在寻找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)

This will split each phrase into words for you. 这将为您将每个短语分解为单词。

 words = ['i am sam ', 'sam i am ', ' green eggs and ham'] for string in words: for str in string.split(): print(str) print() 

I try to add more than one space in your words 我尝试在您的文字中添加多个空格

By the way this is my first python program thanks to you:) 顺便说一下,这是我的第一个python程序,感谢您:)

Here is the solution: 解决方法如下:

for i in words:
        print i
        k=i.split(' ')
        print k

i am sam 我是山姆

['i', 'am', 'sam'] ['我是山姆']

sam i am 萨姆,我是

['sam', 'i', 'am'] ['sam','i','am']

green eggs and ham 绿鸡蛋和火腿

['green', 'eggs', 'and', 'ham'] [“绿色”,“鸡蛋”,“和”,“火腿”]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM