[英]Python - Split strings into words within a 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' theres something on my visa']]
我有以下問題:首先,這些u'
出現在每個子列表的前面嗎? 其次,如何將子列表分成單獨的單詞,即具有以下內容:
[[why, not, giving, me, service], [option, to], [removing, an], [verify, name, and], [my, credit, card], [credit, card], [theres, something, on, my, visa]]
我已經嘗試了split
函數,但是收到以下錯誤消息: AttributeError: 'list' object has no attribute 'split'
。
使用str.split()
函數:
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)
輸出:
[['why', 'not', 'giving', 'me', 'service'], ['option', 'to'], ['removing', 'an'], ['verify', 'name', 'and'], ['my', 'credit', 'card'], ['credit', 'card'], ['theres', 'something', 'on', 'my', 'visa']]
碼:
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 = []
for list in list_1:
res.append(str(list[0]).split())
print res
輸出:
[['why', 'not', 'giving', 'me', 'service'], ['option', 'to'], ['removing', 'an'], ['verify', 'name', 'and'], ['my', 'credit', 'card'], ['credit', 'card'], ['theres', 'something', 'on', 'my', 'visa']]
你代表unicode,我希望這能回答你的問題
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]
這是它的輸出:
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']]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.