Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Python
Hallo Everybody,

I have been working on python, and I have got a problem. What is the way to make list of list of tuple, for example, [[(X, Y)]]. I am new on python.

Thanks
Posted 1-May-13 5:18am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Just like that: [[(X, Y)]]. Have you tried it?
  Permalink  
Comments
Yonathan1111 at 1-May-13 10:57am
   
my intention was to Prepare a training Data for the HMM tagger, for a local corpus (ethiopic)

the tagger has the following syntax:

tagger = trainer.train_supervised(tr_data)

where the parameter tr_data should be in the format of List of List of Tuples

like, tr_data = [sent1, sent2,...]
where sent = [tuple1, tuple2],
and tuple = (word1, tag1), (word2, tag2),...

i have tried the following script, but all i get is just list of tuples, rather than list of list of tuples.

here is my script


import nltk;

wordtag = open("B:\Python\Teacher\Amharic_tagged_tr_corpus.txt","r");
wordtag = wordtag.readlines();
wordTagList = [];

for wt in wordtag:
wordTagList.append(wt.strip("\n"));

sentlist = [];

for w in wordTagList:
sentlist.append(nltk.sent_tokenize(w));

tuple = [];
for s in sentlist:
for w in s:
tuple.append(nltk.word_tokenize(w));

tuplelist = [];
for t in tuple:
for w in t:
tuplelist.append(nltk.tag.str2tuple(w));

print tuplelis[0:3] will list list of tuples like, [(w,t),(w,t),...] , where what i need is [[(w,t), (w,t), ...],[],..]

any help?
lewax00 at 1-May-13 11:15am
   
I'm not familiar with "nltk", but assuming "str2tuple" returns a tuple, you're never creating the second level of the list, you're just appending to "tuplelist". So you'll only end up with a list of tuples. At some point you're going to need to create a new list, append that to tuplelist, then add tuples to that new list instead.
Yonathan1111 at 1-May-13 12:47pm
   
Thanks lewax00, I am going to try it. Thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Suvendu Shekhar Giri 285
1 Sergey Alexandrovich Kryukov 245
2 OriginalGriff 210
3 PIEBALDconsult 180
4 Peter Leow 125
0 Sergey Alexandrovich Kryukov 6,425
1 OriginalGriff 5,918
2 Peter Leow 2,514
3 Maciej Los 2,263
4 Abhinav S 2,249


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100