python - 如何根据标准将一个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
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']
在我的示例中,最终目标是获取列表A_new_1
,A_new_2
和A_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 个单独的列表,每个多选选项一个列表。
谢谢,
解决方案
尝试以下操作:
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']]
推荐阅读
- python - Flask 应用程序被多个进程锁定
- ember.js - 如何处理同一资源的多个 ember 模型
- html - 如果我使用 CSS Grid,我应该为每个 HTML 页面创建一个新的 CSS 文件吗?
- jquery - 由于平滑滚动而无法打开灯箱?
- java - 如果我输入一个未在数组中定义的值,为什么二进制搜索方法会给我一个错误?
- mysql - 选择今年的月份 MySql
- javascript - javascript 元音转大写,辅音转小写
- android - 当用户退出/关闭应用程序时如何停止作业服务?
- python - 某些 python 库的 pipenv 安装错误
- windows - 可以覆盖 Windows 加载时动态链接吗?