Python-将字符串拆分为列表列表中的单词 [英] Python - Split strings into words within a list of lists

查看:1217
本文介绍了Python-将字符串拆分为列表列表中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下列表():

[[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'出现在我的每个子列表的前面吗? 其次,如何将子列表分成单独的单词,即具有类似这样的内容:

and I have the following questions: First, what are these u' appearing in front of every of my sublists? Second, how can I plit my sublists into separate words, i.e. have something like this:

 [[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' 非常感谢.

I already tried the split function, but I get the following error message: AttributeError: 'list' object has no attribute 'split' Thanx a lot.

推荐答案

代码:

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

u'代表unicode,我希望这能回答您的问题

u' represents unicode, I hope this answers your question

这篇关于Python-将字符串拆分为列表列表中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆