Ad

Getting Values From For Loop At Constant Difference

- 1 answer

I have a list like this:

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

The above list will always follow a specific pattern like this :

1st element = A question

2nd element = Options for questions

3rd element = correct answer

4th element = marks from question

5th element = Time alloted

I want to make 5 different lists from the above list with all Questions in 1st list, all options in 2nd list, all correct answer in 3rd list, Marks for each question in one list and Time alloted in 5th list.

Is there a way i can do it?

desired output=

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

I tried to solve it by using the following code:

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

But it throws following error:

Time = my_list[i+4]
IndexError: list index out of range
Ad

Answer

There are a few adjustment you need to make:

  • don't increase i manually in the loop but use step value in range(start, end, step). The step value here would need to be 5.
  • You need to append the results to the list, otherwise you are just replacing the value over and over again and only the last value will be stored in the variables at the end of the for loop.
  • you probably want to strip the newlines \n (but this is optional) using rstrip("\n").
  • you probably also want to have all the possibilities for an answer in an array (but this is also optional). You can do that by using split("#") on the relevant string.
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)

Expected output:

['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']
Ad
source: stackoverflow.com
Ad