扭曲列表的列表

时间:2015-10-15 23:15:21

标签: python regex list tokenize flatten

我有以下数据结构:

 a= [
       [u'happy', u'thursday', u'from', u'my', u'big', u'sweater', u'and', u'this', 
        u'ART', u'@', u'East', u'Village', u',', u'Manhattan', u'https', 
        u':', u'//t.co/5k8PUInmqK'],
       [u'RT', u'@', u'MayorKev', u':', u'IM', u'SO', u'HYPEE', u'@', u'calloutband', 
        u'@', u'FreakLikeBex', u'#', u'Callout', u'#', u'TheBitterEnd', u'#',
        u'Manhattan', u'#', u'Music', u'#', u'LiveMusic', u'#', u'NYC', 
         u'#', u'NY', u'#',
        u'Jersey', u'#', u'NJ', u'http', u':', u'//t.co/0\u2026']
     ]

我看到这个的方式,它是一个字符串列表列表,除了它被一对[]而不是()包围。这对[]是系统生成的结果:

a = [nltk.tokenize.word_tokenize(tweetL) for tweetL in tweetList]

最终,我需要将这个结构展平为一个字符串列表,并对单词进行一些正则表达式和计数操作,但外部的[]对阻止了这一点。

我试图使用:

list.extend()

ll = len(a)
for n in xrange(ll):
    print 'list - ', a[n], 'number = ', n

但仍然得到相同的结果:

list - [ number =  1
list - u number =  2
list - ' number =  3
list - h number =  4
list - a number =  5
list - p number =  6
list - p number =  7

如您所见,代码将字符串的每个符号视为列表元素,而不是将整个字符串视为元素

可以有效地做些什么?

尝试了这个:

flat_list = [i for sublist in a for i in sublist] 
for i in flat_list:
    print 'element - ', i

结果(部分):

element -  h
element -  a
element -  p
element -  p
element -  y
element -   
element -  t

4 个答案:

答案 0 :(得分:2)

我不确定我是否理解你的问题,如果我离开了,请告诉我,但是,根据您提供的输入,您有一个列表清单。不仅如此,如果这是你一直拥有的结构,你可以用

来取出你需要的东西
a = a[0]

那只会给你单一的清单。

然后你可以简单地迭代为:

for i in a:
    print(i)

但是,如果那只是一个样本,你实际上有这样的事情:

[[],[],[],[]]

你想把它完全扁平化为一个列表,那么你想要使用的理解是这样的:

flat_list = [i for sublist in a for i in sublist] 

然后你只需要一个列表:[1, 2, 3, 4]

然后你只需迭代你想要的东西:

for i in flat_list:
    print(i)

或者,如果您想要打印出索引,那么您可以这样做:

for i, v in enumerate(flat_list):
    print("{}: {}".format(i, v))

关于您对extend的使用的最后评论。

extend作为方法的帮助说明:

extend(...)
    L.extend(iterable) -- extend list by appending elements from the iterable

所以,它的用法是“扩展”列表,如下例所示:

a = [1, 2, 3]
b = [4, 5, 6]
a.extend(b)
# a will now be [1, 2, 3, 4, 5, 6]

运行输入:

a = [[u'happy', u'thursday', u'from', u'my', u'big', u'sweater', u'and', u'this', u'ART', u'@', u'East', u'Village', u',', u'Manhattan', u'https', u':', u'//t.co/5k8PUInmqK'], [u'RT', u'@', u'MayorKev', u':', u'IM', u'SO', u'HYPEE', u'@', u'calloutband', u'@', u'FreakLikeBex', u'#', u'Callout', u'#', u'TheBitterEnd', u'#', u'Manhattan', u'#', u'Music', u'#', u'LiveMusic', u'#', u'NYC', u'#', u'NY', u'#', u'Jersey', u'#', u'NJ', u'http', u':', u'//t.co/0\u2026']]

在我的代码上,产生这个输出:

0: happy
1: thursday
2: from
3: my
4: big
5: sweater
6: and
7: this
8: ART
9: @
10: East
11: Village
12: ,
13: Manhattan
14: https
15: :
16: //t.co/5k8PUInmqK

答案 1 :(得分:1)

a= [[u'happy', u'thursday', u'from', u'my', u'big', u'sweater', u'and', u'this', u'ART', u'@', u'East', u'Village', u',', u'Manhattan', u'https', u':', u'//t.co/5k8PUInmqK'], [u'RT', u'@', u'MayorKev', u':', u'IM', u'SO', u'HYPEE', u'@', u'calloutband', u'@', u'FreakLikeBex', u'#', u'Callout', u'#', u'TheBitterEnd', u'#', u'Manhattan', u'#', u'Music', u'#', u'LiveMusic', u'#', u'NYC', u'#', u'NY', u'#', u'Jersey', u'#', u'NJ', u'http', u':', u'//t.co/0\u2026']]

from itertools import chain

flat_a = list(chain.from_iterable(a))

['happy', 'thursday', 'from', 'my', 'big', 'sweater', 'and', 'this', 'ART', '@', 'East', 'Village', ',', 'Manhattan', 'https', ':', '//t.co/5k8PUInmqK', 'RT', '@', 'MayorKev', ':', 'IM', 'SO', 'HYPEE', '@', 'calloutband', '@', 'FreakLikeBex', '#', 'Callout', '#', 'TheBitterEnd', '#', 'Manhattan', '#', 'Music', '#', 'LiveMusic', '#', 'NYC', '#', 'NY', '#', 'Jersey', '#', 'NJ', 'http', ':', '//t.co/0…']

print(flat_a)

答案 2 :(得分:1)

a= [[u'happy', u'thursday', u'from', u'my', u'big', u'sweater', u'and', u'this', u'ART', u'@', u'East', u'Village', u',', u'Manhattan', u'https', u':', u'//t.co/5k8PUInmqK'], [u'RT', u'@', u'MayorKev', u':', u'IM', u'SO', u'HYPEE', u'@', u'calloutband', u'@', u'FreakLikeBex', u'#', u'Callout', u'#', u'TheBitterEnd', u'#', u'Manhattan', u'#', u'Music', u'#', u'LiveMusic', u'#', u'NYC', u'#', u'NY', u'#', u'Jersey', u'#', u'NJ', u'http', u':', u'//t.co/0\u2026']]
for L in a:
    for e in L:
        print "element "+e


element happy
element thursday
element from
element my
element big
element sweater
element and
element this
element ART
element @
element East

答案 3 :(得分:1)

嵌套列表理解应解决您的第一个问题。

a = [token for tweetL in tweetList for token in nltk.tokenize.word_tokenize(tweetL)]

此构造允许您迭代从嵌套for循环中找到的元素。最外层的for循环始终是第一个,然后是第二个最外层的等等,直到最后一个循环到最后。

这可能有助于理解这相当于:

a = []
for tweetL in tweetList:
    for token in nltk.tokenize.word_tokenize(tweetL):
        a.append(token)

在Python 2中,您可以使用utf-8对unicode字符串进行编码。这会将它们从unicode类型转换为str类型,这应解决UnicodeEncodeError

示例:

u'\u2713'.encode('utf-8')

有关Python 2 Unicode的更多信息,请参阅此处:https://docs.python.org/2/howto/unicode.html

相关问题