convert a list of list into a list of list of tuple

  • A+

I have a list of list consist of:

> [['Di/in/QUE', 'mana/wh/QUE', 'lokasi/nn/INTENT', 'laboratorium/nnp/LOC', 'dasar/nnp/LOC', '?/?/O'], ['Di/in/QUE', 'mana/wh/QUE', 'lokasi/nn/INTENT', 'laboratorium/nnp/LOC', 'dasar/nnp/LOC', '2/nnp/LOC', '?/?/O'], ['Di/in/QUE', 'mana/wh/QUE', 'lokasi/nn/INTENT', 'laboratorium/nnp/LOC', 'lanjut/nnp/LOC', '?/?/O']] 

I want to convert it into a list of list of tuple, like this:

> [[('Di','in','QUE'), ('mana','wh','QUE'), ('lokasi','nn','INTENT'), ('laboratorium','nnp','LOC'), ('dasar','nnp','LOC'), ('?','?','O')], [('Di','in','QUE'), ('mana','wh','QUE'), ('lokasi','nn','INTENT'), ('laboratorium','nnp','LOC'), ('dasar','nnp','LOC'), ('2','nnp','LOC'), ('?','?','O')], [('Di','in','QUE'), ('mana','wh','QUE'), ('lokasi','nn','INTENT'), ('laboratorium','nnp','LOC'), ('lanjut','nnp','LOC'), ('?','?','O')]] 

I read the data from a text file so this is my code:

with open("corpusposner.txt", "r") as f:      vallist = [line.split() for line in f] f.close() standard_form_tokens = [] for sentence in vallist:     for satupsg in sentence:         anotasi = satupsg.split('/')         kata, tag, ner = anotasi[0], anotasi[1], anotasi[2]          standard_form_tokens.append((kata, tag.lower(), ner)) 

When I print standard_form_tokens it return only just one big list of tuple

[('Di', 'in', 'QUE'), ('mana', 'wh', 'QUE'), ('lokasi', 'nn', 'INTENT'), ('laboratorium', 'nnp', 'LOC'), ('dasar', 'nnp', 'LOC'), ('?', '?', 'O'), ('Di', 'in', 'QUE'), ('mana', 'wh', 'QUE'), ('lokasi', 'nn', 'INTENT'), ('laboratorium', 'nnp', 'LOC'), ('dasar', 'nnp', 'LOC'), ('2', 'nnp', 'LOC'), ('?', '?', 'O'), ('Di', 'in', 'QUE'), ('mana', 'wh', 'QUE'), ('lokasi', 'nn', 'INTENT'), ('laboratorium', 'nnp', 'LOC'), ('lanjut', 'nnp', 'LOC'), ('?', '?', 'O')]

I tried to append the standard_form_tokens into a new list but it's not working. Any ideas?


Use tuple with split and a list comprehension:

[[tuple(i.split('/')) for i in j] for j in arr] 


[[('Di', 'in', 'QUE'),   ('mana', 'wh', 'QUE'),   ('lokasi', 'nn', 'INTENT'),   ('laboratorium', 'nnp', 'LOC'),   ('dasar', 'nnp', 'LOC'),   ('?', '?', 'O')],  [('Di', 'in', 'QUE'),   ('mana', 'wh', 'QUE'),   ('lokasi', 'nn', 'INTENT'),   ('laboratorium', 'nnp', 'LOC'),   ('dasar', 'nnp', 'LOC'),   ('2', 'nnp', 'LOC'),   ('?', '?', 'O')],  [('Di', 'in', 'QUE'),   ('mana', 'wh', 'QUE'),   ('lokasi', 'nn', 'INTENT'),   ('laboratorium', 'nnp', 'LOC'),   ('lanjut', 'nnp', 'LOC'),   ('?', '?', 'O')]] 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: