Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 694
Irfan HP Avatar asked Jul 11 '18 04:07

Irfan HP


People also ask

How do you convert a list into a list of tuples?

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.

Can I turn a list into a tuple?

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.

How do you split a list into a tuple?

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.

How do you convert a tuple to a list of tuples?

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.

Can you have a list of list of lists in Python?

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.


2 Answers

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')]]
like image 115
user3483203 Avatar answered Sep 23 '22 16:09

user3483203


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')]]
like image 35
Marcus.Aurelianus Avatar answered Sep 19 '22 16:09

Marcus.Aurelianus