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?
Using the tuple() built-in function An iterable can be passed as an input to the tuple () function, which will convert it to a tuple object. If you want to convert a Python list to a tuple, you can use the tuple() function to pass the full list as an argument, and it will return the tuple data type as an output.
tuple () function can take any iterable as an argument and convert it into a tuple object. As you wish to convert a python list to a tuple, you can pass the entire list as a parameter within the tuple() function, and it will return the tuple data type as an output.
Method #1 : Using map() + split() + tuple() The map function can be used to link the logic to each string, split function is used to split the inner contents of list to different tuple attributes and tuple function performs the task of forming a tuple.
To convert a tuple into list in Python, call list() builtin function and pass the tuple as argument to the function. list() returns a new list generated from the items of the given tuple.
Python provides an option of creating a list within a list. If put simply, it is a nested list but with one or more lists inside as an element. Here, [a,b], [c,d], and [e,f] are separate lists which are passed as elements to make a new list. This is a list of lists.
Use tuple
with split
and a list comprehension:
[[tuple(i.split('/')) for i in j] for j in arr]
Output:
[[('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')]]
The reason why you have only one big list of tuple is that you use two 'for loop' to loop through the list of list but you only append one time at each instance.
Just correct based on your code. You can create an empty temp list, append on it, and then append the temp into your result. Your result then will be a list of lists with tuples inside.
Try this:
standard_form_tokens = []
for sentence in vallist:
temp=[]
for satupsg in sentence:
anotasi = satupsg.split('/')
kata, tag, ner = anotasi
temp.append((kata, tag.lower(), ner))
standard_form_tokens.append(temp)
Output:
[[('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')]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With