gpt4 book ai didi

python - 将列表的列表转换为元组列表的列表

转载 作者:太空狗 更新时间:2023-10-29 19:37:10 24 4
gpt4 key购买 nike

我有一个列表,列表包括:

> [['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']]

我想把它转换成元组列表的列表,像这样:

> [[('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')]]

我从一个文本文件中读取数据,所以这是我的代码:

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))

当我打印 standard_form_tokens 时,它只返回一个大的元组列表

[('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')]

我试图将 standard_form_tokens 附加到新列表中,但它不起作用。有什么想法吗?

最佳答案

tuplesplit 和列表理解结合使用:

[[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')]]

关于python - 将列表的列表转换为元组列表的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51277552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com