我有一個這樣的串列:
my_list = ['Normal adult dogs have how many teeth?\n', '64#32#42#16\n', '42\n', '10\n', '20\n', 'What is the most common training command taught to dogs?\n',
'Sit#Beg#Shake#Catch\n', 'Sit\n', '10\n', '20\n', 'What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n', 'Puppy\n', '10\n', '20', 'Which part of cats is as unique as human fingerprints?\n', 'Paw Prints#Nose Prints#Stripes Color#Eye Colors\n', 'Nose Prints\n', '10\n', '20\n', 'What is a group of
cats called??\n', 'Clowder#Herd#Pack#Flock\n', 'Clowder\n', '10\n', '20\n', 'What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n', 'Cat\n', '10\n',
'20']
上面的串列將始終遵循這樣的特定模式:
第一個元素 = 一個問題
第二個元素 = 問題選項
第三個元素 = 正確答案
第 4 個元素 = 問題的分數
第 5 個元素 = 分配的時間
我想從上面的串列中制作 5 個不同的串列,所有問題都在第一個串列中,所有選項在第二個串列中,所有正確答案在第三個串列中,每個問題的分數在一個串列中,時間分配在第 5 個串列中。
有辦法我可以做到嗎?
所需的輸出=
ques = [my_list[0],my_list[5],my_list[10]....]
options = [my_list[1],my_list[6],my_list[11]....]
correct_answer = [my_list[2],my_list[7],my_list[12]....]
Marks = [my_list[3],my_list[8],my_list[13]....]
Time = [my_list[4],my_list[9],my_list[14]....]
我試圖通過使用以下代碼來解決它:
for i in range(0,len(my_list)):
ques = my_list[i]
options = my_list[i 1]
correct_answer = my_list[i 2]
Marks = my_list[i 3]
Time = my_list[i 4]
i 5
但它會引發以下錯誤:
Time = my_list[i 4]
IndexError: list index out of range
uj5u.com熱心網友回復:
您需要進行一些調整:
- 不要
i在回圈中手動增加,而是在range(start, end, step). 這里的步長值需要為 5。 - 您需要將結果附加到串列中,否則您只是一遍又一遍地替換該值,并且只有最后一個值將存盤在
for loop. - 您可能想
\n使用rstrip("\n"). - 您可能還希望在陣列中獲得答案的所有可能性(但這也是可選的)。您可以通過
split("#")在相關字串上使用來做到這一點。
my_list = ['Normal adult dogs have how many teeth?\n', '64#32#42#16\n', '42\n', '10\n', '20\n',
'What is the most common training command taught to dogs?\n',
'Sit#Beg#Shake#Catch\n', 'Sit\n', '10\n', '20\n', 'What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n',
'Puppy\n', '10\n', '20', 'Which part of cats is as unique as human fingerprints?\n',
'Paw Prints#Nose Prints#Stripes Color#Eye Colors\n', 'Nose Prints\n', '10\n', '20\n',
'What is a group of cats called??\n', 'Clowder#Herd#Pack#Flock\n', 'Clowder\n', '10\n', '20\n',
'What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n', 'Cat\n', '10\n',
'20']
ques = []
options = []
correct_answer = []
Marks = []
Time = []
for i in range(0,len(my_list), 5):
ques.append(my_list[i].rstrip("\n"))
# split all the answer possibilites within the str and store them in an array
options.append(my_list[i 1].rstrip("\n").split("#"))
correct_answer.append(my_list[i 2].rstrip("\n"))
Marks.append(my_list[i 3].rstrip("\n"))
Time.append(my_list[i 4].rstrip("\n"))
print(ques)
print(options)
print(correct_answer)
print(Marks)
print(Time)
預期輸出:
['Normal adult dogs have how many teeth?', 'What is the most common training command taught to dogs?', 'What is this?;{;i1.png;};', 'Which part of cats is as unique as human fingerprints?', 'What is a group of cats called??', 'What is this?;{;i1.png;};']
[['64', '32', '42', '16'], ['Sit', 'Beg', 'Shake', 'Catch'], ['Lion', 'Dog', 'Cat', 'Puppy'], ['Paw Prints', 'Nose Prints', 'Stripes Color', 'Eye Colors'], ['Clowder', 'Herd', 'Pack', 'Flock'], ['Lion', 'Dog', 'Cat', 'Puppy']]
['42', 'Sit', 'Puppy', 'Nose Prints', 'Clowder', 'Cat']
['10', '10', '10', '10', '10', '10']
['20', '20', '20', '20', '20', '20']
uj5u.com熱心網友回復:
有多種方法可以做到這一點。您想要做的是將您的串列分成多個塊。一種沒有任何庫支持的方法將如下所示
my_list = [my_list[x: x 5] for x in range(0, len(my_list), 5)]
這將給出一個輸出
[['Normal adult dogs have how many teeth?\n', '64#32#42#16\n', '42\n', '10\n', '20\n'], ['What is the most common training command taught to dogs?\n', 'Sit#Beg#Shake#Catch\n', 'Sit\n', '10\n', '20\n'], ['What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n', 'Puppy\n', '10\n', '20'], ['Which part of cats is as unique as human fingerprints?\n', 'Paw Prints#Nose Prints#Stripes Color#Eye Colors\n', 'Nose Prints\n', '10\n', '20\n'], ['What is a group of cats called??\n', 'Clowder# Herd#Pack#Flock\n', 'Clowder\n', '10\n', '20\n'], ['What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n', 'Cat\n', '10\n', '20']]
其他選項使用itertools或打開more_itertools
uj5u.com熱心網友回復:
您的 for 回圈失敗的原因是您超出了陣列的邊界。就像您的范圍從頭到尾一樣,然后您想訪問 [end of the list] 1 aso 這顯然不起作用,因為您已經在末尾了。
也適用于您的其他方法:
ques = [my_list[0],my_list[5],my_list[10]....]
options = [my_list[1],my_list[6],my_list[11]....]
correct_answer = [my_list[2],my_list[7],my_list[12]....]
Marks = [my_list[3],my_list[8],my_list[13]....]
Time = [my_list[4],my_list[9],my_list[14]....]
您實際上可以這樣做,盡管我寧愿使用切片運算子。因此,您可以像這樣對串列進行切片name[start:end:stepsize],如果您將其中一個留空,而不是默認值是 start、end 和 1。例如name[5:],會給您從 5 到 endname[:5]的元素,相反地為您提供從 start 到元素之前的元素5.
因此,在您的情況下,要獲得每個第 4 個元素,您name[3::5]將從第 4 個元素(從 0 開始)開始,然后采取 5 步直到結束。
uj5u.com熱心網友回復:
my_list = ['Normal adult dogs have how many teeth?\n', '64#32#42#16\n', '42\n', '10\n', '20\n', 'What is the most common training command taught to dogs?\n',
'Sit#Beg#Shake#Catch\n', 'Sit\n', '10\n', '20\n', 'What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n', 'Puppy\n', '10\n', '20', 'Which part of cats is as unique as human fingerprints?\n', 'Paw Prints#Nose Prints#Stripes Color#Eye Colors\n', 'Nose Prints\n', '10\n', '20\n', "What is a group of cats called??\n", 'Clowder#Herd#Pack#Flock\n', 'Clowder\n', '10\n', '20\n', 'What is this?;{;i1.png;};\n', 'Lion#Dog#Cat#Puppy\n', 'Cat\n', '10\n',
'20']
ques, options, correct_answer, marks, time = [], [], [], [], []
for n in range(1, (len(my_list)//5) 1):
ques.append(my_list[5*(n-1)].rstrip("\n"))
options.append(my_list[5*n - 4].rstrip("\n").split("#"))
correct_answer.append(my_list[5*n - 3].rstrip("\n"))
marks.append(my_list[5*n - 2].rstrip("\n"))
time.append(my_list[5*n - 1].rstrip("\n"))
print("Ques:", ques)
print("Options:", options)
print("Correct_answer:", correct_answer)
print("Marks:", marks)
print("Time:", time)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/449705.html
標籤:Python python-3.x 列表 循环
