Home > Enterprise >  Getting values from for loop at constant difference
Getting values from for loop at constant difference

Time:03-23

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

CodePudding user response:

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

CodePudding user response:

There are multiple ways to do it. What you want to do is split your list to even chunks. One approach without any library support will be to like below

my_list = [my_list[x: x 5] for x in range(0, len(my_list), 5)]

This will give an output

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

Other options are open using itertools or more_itertools

CodePudding user response:

The reason why your for loop fails is because you exceed the boundaries of your array. Like your range goes from start to finish and then you want to access the [end of the list] 1 aso which obviously doesn't work as you're already at the end.

Also for your other approach:

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

you can actually do that sort of, though I'd rather use the slice operators. So you can slice your list like this name[start:end:stepsize] and if you leave one of those empty than the defaults are start, end and 1. So for example name[5:] would give you the elements from 5 to end, conversely name[:5] gives you the elements from start to the element before 5.

So in your case to get every 4th element you go with name[3::5] this starts at the 4th element (starting at 0) and then takes steps of 5 till it reaches the end.

  • Related