按可用连字符数追加列表项


mylist = [('country', 'NN'), ('shoot', 'NN-DT-PPL'), ('threats', 'NN-JJ'), ('both','RB-JJ-NN'), ('during', 'NN-VBD-JJ-RB'), ('former', 'NN-RB'), ('school', 'NN-CC-JJ-DT'), 
    ('teacher', 'NN-VBZ-PPL-JJ-DT'), ('receive', 'VBZ'), ('batman', 'NN-IN-ABX-CD-RB')]

我有一个名为mylist的列表。它由带有单词及其随机标签的元组组成。我不想使用正则表达式。最小标记为 1,最大标记为 5。我想根据标签的数量有 5 个不同的列表。

对于一个标签元组,我尝试了这个:

one=[] for i in mylist: if '-' not in i[1]: one.append(i) print one

正确打印[('country', 'NN'), [('receive', 'VBZ')

对于第二个标签,我希望打印[('threats', 'NN-JJ'), [('former', 'NN-RB')

以此类推,用于第三、第四和第五个标签集。我不知道该怎么做。

我的实际文件有n数量的标签,它由大约 1000 万个单词及其标签组成。有没有办法让我们知道哪个词具有最大的不同标签?

这将有很大帮助!

您可以使用

defaultdict来组织数据,并使用.count来计算-的数量。

from collections import defaultdict
mylist = [('country', 'NN'), ('shoot', 'NN-DT-PPL'), ... ]
res = defaultdict(list)
for item, tags in mylist:
    res[tags.count('-') + 1].append((item, tags))

您可以使用以下代码打印结果。

for k, v in res.items():
    print(str(k) + ": " + str(v))

指纹:

brunsgaard@archbook /tmp> python test2.py
1: [('country', 'NN'), ('receive', 'VBZ')]
2: [('threats', 'NN-JJ'), ('former', 'NN-RB')]
3: [('shoot', 'NN-DT-PPL'), ('both', 'RB-JJ-NN')]
4: [('during', 'NN-VBD-JJ-RB'), ('school', 'NN-CC-JJ-DT')]
5: [('teacher', 'NN-VBZ-PPL-JJ-DT'), ('batman', 'NN-IN-ABX-CD-RB')]

其他方法可以做到这一点

from itertools import groupby
from operator import itemgetter

a=[('country', 'NN'), ('shoot', 'NN-DT-PPL'), ('threats', 'NN-JJ'), ('both','RB-JJ-NN'), ('during', 'NN-VBD-JJ-RB'), ('former', 'NN-RB'), ('school', 'NN-CC-JJ-DT'),
    ('teacher', 'NN-VBZ-PPL-JJ-DT'), ('receive', 'VBZ'), ('batman', 'NN-IN-ABX-CD-RB')]

func=lambda x:len(x[1].split('-'))
for k,g in groupby(sorted(a,key=func),key=func):
    print k,list(g)
#0utput
1 [('country', 'NN'), ('receive', 'VBZ')]
2 [('threats', 'NN-JJ'), ('former', 'NN-RB')]
3 [('shoot', 'NN-DT-PPL'), ('both', 'RB-JJ-NN')]
4 [('during', 'NN-VBD-JJ-RB'), ('school', 'NN-CC-JJ-DT')]
5 [('teacher', 'NN-VBZ-PPL-JJ-DT'), ('batman', 'NN-IN-ABX-CD-RB')]
mylist = [('country', 'NN'), ('shoot', 'NN-DT-PPL'), ... ]
res = defaultdict(list)
for item, tags in mylist:
    res[tags.count('-') + 1].append((item, tags))

最大破折号计数为:

max_dash_count = max(i[1].count('-') for i in mylist) + 1

但是,使用字典有更有效的方法可以做到这一点:

dash_dict = dict()
for i in mylist:
    count = i[1].count('-') + 1
    if count in dash_dict:
        dash_dict[count].add(i)
    else:
        dash_dict[count] = [i]

之后,您将得到一个列表字典,您可以轻松迭代:

for count in sorted(dash_dict.keys()):
    print 'Items with ' + str(count) + ' dashes:'
    for i in dash_dict[count]:
        print repr(i)
#!/usr/bin/python
mylist = [('country', 'NN'), ('shoot', 'NN-DT-PPL'), ('threats', 'NN-JJ'), ('both','RB-JJ-NN'), ('during', 'NN-VBD-JJ-RB'), ('former', 'NN-RB'), ('school', 'NN-CC-JJ-DT'), ('teacher', 'NN-VBZ-PPL-JJ-DT'), ('receive', 'VBZ'), ('batman', 'NN-IN-ABX-CD-RB')]
MAX_TAG = 5
def findTag():
   d = {}
   for tup in mylist:
      a,b = tup
      n = b.count('-')
      if not 0 <= n <= MAX_TAG - 1:
         continue
      if n not in d:
         d[n] = []
      d[n].append(tup)
   for k in sorted(d.keys()):
      print '{} => {}'.format(k+1, d[k])
if __name__ == '__main__':
   findTag()
1 => [('country', 'NN'), ('receive', 'VBZ')]
2 => [('threats', 'NN-JJ'), ('former', 'NN-RB')]
3 => [('shoot', 'NN-DT-PPL'), ('both', 'RB-JJ-NN')]
4 => [('during', 'NN-VBD-JJ-RB'), ('school', 'NN-CC-JJ-DT')]
5 => [('teacher', 'NN-VBZ-PPL-JJ-DT'), ('batman', 'NN-IN-ABX-CD-RB')]

您可以使用"-"作为分隔符拆分字符串,并计算结果生成的列表中的元素数量,如下所示(对于 3 个标签( -

>>> [t for t in mylist if len(t[1].split('-')) == 3]
[('shoot', 'NN-DT-PPL'), ('both', 'RB-JJ-NN')]

最新更新