![](/img/trans.png)
[英]How to separate a Python list into two lists, according to some aspect of the elements
[英]How to separate one python list into 3 different lists according to the criteria
我有一個像下面這樣的python列表:
A = ['"','<bos>', 'What', 'colour', 'is', 'the', 'sky','<spec_token>' ,'(A)', 'red','<spec_token>', '(B)', 'blue', '<spec_token>','(C)', 'yellow','<eos>', '"']
對於列表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']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.