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 Kornfeld Eliyahu Peter 275
1 Maciej Los 204
2 Sergey Alexandrovich Kryukov 200
3 King Fisher 185
4 OriginalGriff 183
0 OriginalGriff 6,223
1 DamithSL 4,724
2 Maciej Los 4,196
3 Kornfeld Eliyahu Peter 3,894
4 Sergey Alexandrovich Kryukov 3,496


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 1 May 2013
Copyright © CodeProject, 1999-2014
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