1

I'm new to python and would greatly appreciate some help. 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.
  • just a reminder, when you use split(), no parameter is needed, or for multiple space case it will fail ... – shole May 14 '15 at 04:33

9 Answers9

1

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.

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)

Andrew Malta
  • 850
  • 5
  • 15
1
Try this:

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

shruti1810
  • 3,920
  • 2
  • 16
  • 28
1

You will need to call split:

for element in words:
    for word in element.split(' '):
        print word
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

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']

into 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.

If you wanted you could do words = (' '.join(words)).split() if you wanted to overwrite the old list

Burkely91
  • 902
  • 9
  • 28
0

You have an extra for loop in there.

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.

deluxes
  • 36
  • 5
0

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

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']

Further you can check Python regex separate space-delimited words into a list and this one How to split a string into a list?

Community
  • 1
  • 1
Fatti Khan
  • 1,543
  • 1
  • 11
  • 19
0

I think you're looking for the split() function:

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.

Brent Washburne
  • 12,904
  • 4
  • 60
  • 82
0

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:)

shole
  • 4,046
  • 2
  • 29
  • 69
0

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']

green eggs and ham

['green', 'eggs', 'and', 'ham']

Jay Venkat
  • 397
  • 2
  • 5
  • 18