Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 solution

Just like that: [[(X, Y)]]. Have you tried it?
 
Share this answer
 
Comments
Yonathan1111 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900