Skip to content Skip to sidebar Skip to footer

Append List Items By Number Of Hyphens Available

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'), ('t

Solution 1:

You can use a defaultdict to organize the data and .count to count the number of -.

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))

You can print the result with the following code.

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

prints:

brunsgaard@archbook /tmp> pythontest2.py1: [('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')]

Solution 2:

Other way of doing this

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)

#0utput1 [('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')]

Solution 3:

mylist = [('country', 'NN'), ('shoot', 'NN-DT-PPL'), ... ]
res = defaultdict(list)

for item, tags in mylist:
    res[tags.count('-') + 1].append((item, tags))

Solution 4:

Maximum dash count would be:

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

However, there are more efficient ways to do this, using dictionaries:

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]

Afterward, you're left with a dictionary of lists you can easily iterate over:

for count insorted(dash_dict.keys()):
    print'Items with ' + str(count) + ' dashes:'for i in dash_dict[count]:
        printrepr(i)

Solution 5:

#!/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:
         continueif 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')]

Post a Comment for "Append List Items By Number Of Hyphens Available"