Skip to content Skip to sidebar Skip to footer

Python - Split Strings Into Words Within A List Of Lists

I have the following list of lists (): [[u' why not giving me service'], [u' option to'], [u' removing an'], [u' verify name and '], [u' my credit card'], [u' credit card'], [u' th

Solution 1:

With str.split() function:

l = [[u' why not giving me service'], [u' option to'], [u' removing an'], [u' verify name and '], [u' my credit card'], [u' credit card'], [u' theres something on my visa']]

result = [_[0].split() for _ in l]
print(result)

The output:

[['why', 'not', 'giving', 'me', 'service'], ['option', 'to'], ['removing', 'an'], ['verify', 'name', 'and'], ['my', 'credit', 'card'], ['credit', 'card'], ['theres', 'something', 'on', 'my', 'visa']]

Solution 2:

Code:

list_1 = [[u' why not giving me service'], [u' option to'], [u' removing an'], [u' verify name and '], [u' my credit card'], [u' credit card'], [u' theres something on my visa']]
res = []
forlistin list_1:
    res.append(str(list[0]).split())

print res

Output:

[['why', 'not', 'giving', 'me', 'service'], ['option', 'to'], ['removing', 'an'], ['verify', 'name', 'and'], ['my', 'credit', 'card'], ['credit', 'card'], ['theres', 'something', 'on', 'my', 'visa']]

u' represents unicode, I hope this answers your question

Solution 3:

x=[[u' why not giving me service'], [u' option to'], [u' removing an'], [u' verify name and '], [u' my credit card'], [u' credit card'], [u' theres something on my visa']]
[[y.split() for y in m] for m in x]

here's the output of it:

In [3]: [[y.split() for y in m] for m in x]
Out[3]: 
[[[u'why', u'not', u'giving', u'me', u'service']],
 [[u'option', u'to']],
 [[u'removing', u'an']],
 [[u'verify', u'name', u'and']],
 [[u'my', u'credit', u'card']],
 [[u'credit', u'card']],
 [[u'theres', u'something', u'on', u'my', u'visa']]]

Post a Comment for "Python - Split Strings Into Words Within A List Of Lists"