[英]How to separate one python list into 3 different lists according to the criteria
I have a python list like below:我有一个像下面这样的python列表:
A = ['"','<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(A)', 'red','<spec_token>', '(B)', 'blue', '<spec_token>','(C)', 'yellow','<eos>', '"']
For list A
, what is the easiest way to do the followings?对于列表A
,执行以下操作的最简单方法是什么?
A_new = ['<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(A)', 'red','<spec_token>', '(B)', 'blue', '<spec_token>','(C)', 'yellow','<eos>']
A
into 3 lists, one for each multiple choice option, ie the output should be like below:将A
分成 3 个列表,每个多选选项一个,即输出应如下所示:A_new_1 = ['<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(A)', 'red']
A_new_2 = ['<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(B)', 'blue']
A_new_3 = ['<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(C)', 'yellow']
In my example, the ultimate goal is to get the lists A_new_1
, A_new_2
and A_new_3
.在我的示例中,最终目标是获取列表A_new_1
、 A_new_2
和A_new_3
。
I am currently working on making python function to achieve this objective, and my code so far is the following:我目前正在制作 python 函数来实现这个目标,到目前为止我的代码如下:
# 2. for GPT2MCHeadModel (ARC, openbookQA)
def GPT2MCHeadModel_data_manipulator(file_path):
f = open(file_path, "r")
ln = f.readline()
ln = ln.replace('"', '') # remove unnecessary quotation marks from the raw text file.
ln_split = ln.split()
# insert appropriate tokens into the raw text files before processing them in GPT2MCHeads model.
ln_split.insert(0, "<bos>")
ln_split.insert(len(ln_split) - 1, "<eos>")
ln_split.insert(ln_split.index("(A)"), "<mcOption>")
ln_split.insert(ln_split.index("(B)"), "<mcOption>")
ln_split.insert(ln_split.index("(C)"), "<mcOption>")
ln_split.insert(ln_split.index("(D)"), "<mcOption>")
and I am not sure how to separate the contents into 3 separate lists, one list for each multiple choice option.而且我不确定如何将内容分成 3 个单独的列表,每个多选选项一个列表。
Thank you,谢谢,
Try the following:请尝试以下操作:
A = ['"','<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(A)', 'red','<spec_token>', '(B)', 'blue', '<spec_token>','(C)', 'yellow','<eos>', '"']
# Problem 1
A = [x for x in A if x != '"']
i = A.index("<spec_token>")
c = A.count("<spec_token>")
# Problem 2
output = [A[:i] + A[i+j*3:i+j*3+3] for j in range(c)]
Output输出
>>> A
['<bos>', 'What', 'colour', 'is', 'the', 'sky', '<spec_token>', '(A)', 'red', '<spec_token>', '(B)', 'blue', '<spec_token>', '(C)', 'yellow', '<eos>']
>>> output
[['<bos>', 'What', 'colour', 'is', 'the', 'sky', '<spec_token>', '(A)', 'red'],
['<bos>', 'What', 'colour', 'is', 'the', 'sky', '<spec_token>', '(B)', 'blue'],
['<bos>', 'What', 'colour', 'is', 'the', 'sky', '<spec_token>', '(C)', 'yellow']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.