ml_student ml_student - 7 months ago 11
Python Question

How to create a nested list from an object?

I have an tags object from treetagger's python wrapper that apparently is list:

In:
print type (tags)
Out:
<type 'list'>


When I print the content of
tags
as follows, I get the following lists:
Out:

[[u'I\tPP\tI', u'am\tVBP\tbe', u'an\tDT\tan', u'amateur\tJJ\tamateur']]


[[u'This\tDT\tthis', u'my\tPP$\tmy']]


[[u'was\tVBD\tbe', u'to\tTO\tto', u'be\tVB\tbe', u'my\tPP$\tmy', u'camera\tNN\tcamera', u'for\tIN\tfor', u'long-distance\tJJ\tlong-distance', u'backpacking\tNN\tbackpacking', u'trips\tNNS\ttrip', u'.\tSENT\t.', u'It\tPP\tit']]


However, I would like to get just one single nested list like this:

[[u'I\tPP\tI', u'am\tVBP\tbe', u'an\tDT\tan', u'amateur\tJJ\tamateur'],[u'This\tDT\tthis', u'my\tPP$\tmy'],[u'was\tVBD\tbe', u'to\tTO\tto', u'be\tVB\tbe', u'my\tPP$\tmy', u'camera\tNN\tcamera', u'for\tIN\tfor', u'long-distance\tJJ\tlong-distance', u'backpacking\tNN\tbackpacking', u'trips\tNNS\ttrip', u'.\tSENT\t.', u'It\tPP\tit']]


I all ready tried with
list()
,
append()
,
[]
and also with:

for sublist in [item]:
new_list = []
new_list.append(sublist)
print new_list


Any idea of how can I nest each list from
tags
?.

Answer

This is a list of one element (another list).

[[u'I\tPP\tI', u'am\tVBP\tbe', u'an\tDT\tan', u'amateur\tJJ\tamateur']]

So if item is a list of lists, each with one element, then you can do

new_list = [sublist[0] for sublist in item]

If you had more than one element in each sublist, then you'll need another nested loop in that.


Though, in reality, you shouldn't use lines = [f.read()]. The documentation uses a single string when you use tag_text, so start with this

# Initialize one tagger
tagger = treetaggerwrapper.TreeTagger(TAGLANG='en')
# Loop over the files
all_tags = []
for filename in sorted(glob.glob(os.path.join(input_directory, '*.txt'))):      
    with codecs.open(filename, encoding='utf-8') as f:
        # Read the file
        content = f.read()
        # Tag it
        tags = tagger.tag_text(content)
        # add those tags to the master tag list
        all_tags.append(tags)

print(all_tags)