Hi,
I have a list of lists taken from a text file.
I would like to add the elements that are in the second and third position,but these are not simple elements, but represent the position of another element that is found in the sublist.
For example:
Then the value I got must go to the third position, because the element 3 always represents the index,where you will have to put the new item.
This is my code for now....
Thanks and regards,
RavCoder
I have a list of lists taken from a text file.
I would like to add the elements that are in the second and third position,but these are not simple elements, but represent the position of another element that is found in the sublist.
For example:
1 2 3 4 5 |
list = [ 1 , 0 , 0 , 3 ] list [ 1 ] = 0 list [ 2 ] = 0 sum = 1 + 1 - - > 2 # because zero corresponds to position 0 which has as its element 1 |
1 |
list = [ 1 , 0 , 0 , 2 ] |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#Convert text in a list #def split_four(x): file = open ( 'data.txt' ) lines = file .read().split( ',' ) #print(lines) #split into sublist with four items n = 4 m = 1 split_four = [lines[i:i + n] for i in range ( 0 , len (lines), n - m)] #print(split_four) for x in split_four: for y in range ( 0 , len (x)): # sum index = element |
RavCoder