Issue
I do not understand why do I get only the last string of the original list? thx
documents = ['Hello, how are you!',
'Win money, win from home.',
'Call me now.',
'Hello, Call hello you tomorrow?']
lc =[]
for x in documents:
lc = x.lower()
print (lc)
Out: hello, call hello you tomorrow?
Solution
Your code first assigns lc
to an empty array. Then you loop over the original array, and each time through you throw away whatever lc
is assigned to (starting with that unused empty array) and replace it with the lowercase version of that particular string. At the end you leave lc
with the lowercase version of the final string; everything else has been discarded.
What you want to do instead is build a new array from the old one. The pythonic way to do that is with a list comprehension.
lc = [x.lower() for x in documents]
That makes a new array that contains the lowercase version of each element in the original array:
>>> lc
['hello, how are you!', 'win money, win from home.',
'call me now.', 'hello, call hello you tomorrow?']
Answered By - Mark Reed
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.