首页 > 解决方案 > 如何根据标准将一个python列表分成3个不同的列表

问题描述

我有一个如下的 python 列表:

A = ['"','<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(A)', 'red','<spec_token>', '(B)', 'blue', '<spec_token>','(C)', 'yellow','<eos>', '"']

对于 list A,执行以下操作的最简单方法是什么?

A_new =  ['<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(A)', 'red','<spec_token>', '(B)', 'blue', '<spec_token>','(C)', 'yellow','<eos>']
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']

在我的示例中,最终目标是获取列表A_new_1,A_new_2A_new_3.

我目前正在制作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>") 

而且我不确定如何将内容分成 3 个单独的列表,每个多选选项一个列表。

谢谢,

标签: pythonstringlistfile-iosplit

解决方案


尝试以下操作:

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

输出

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

推荐阅读