Python Forum

Full Version: item = index position - list of list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:
 
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
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.
 list = [1 , 0, 0, 2]
This is my code for now....
#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 
    
Thanks and regards,
RavCoder
Don’t use list and sum as names.
(Dec-02-2019, 03:13 PM)perfringo Wrote: [ -> ]Don’t use list and sum as names.

I don't understand, can you explain better?
No built-in list() and sum() anymore....
Ok thank you, i was trying to solve the advent of code on day 2.
But I still can't solve it because I'm stuck on how to access the items inside the list.
I've seen several solutions but I don't want to copy them.
You read the data from the file, split it into pieces and convert the string digits into integer values in two lines.
with open("input.txt") as file:
    program = [int(num) for line in file for num in line.strip().split(",")]
Then you have a list of integer values and this is your program.
You need a pointer to the start of each code block, at the beginning this pointer is zero.
So the first opcode is at program[pointer].
You can access the second, third and fourth values with program[pointer+1], program[pointer+2], program[pointer+3].
But there´s a more pythonic way, which is call list unpacking.
index1, index2, index3 = program[pointer+1:pointer+4]
if opcode is equal to 1, program[index3] = program[index1] + program[index2]
After that you need to increase the value of the pointer accordingly, so that the next code block is executed.
I hope that helps. The rest is on you
But how do I create a pointer in Python? I do a loop, right?
By doing the code you showed you have a list of lists,how do I access the list in the list?
I was thinking of a nested loop, but I don't if it's correct to access list
No, there is no list in a list. Just do a print(program).
And in this case pointer is an integer variable, which is used as the index into the list.
Of course you need to create a loop.
In the loop you execute the opcodes and then you need to increase the pointer to the next code block.
In this puzzle that would be pointer += 4.
I have also seen that we need to replace the values ​​in the list with 2 and 12, do I have to do it before or after?
I implemented the code:
#create a list from textfile 
with open("data.txt") as file:
    program = [int(num) for line in file for num in line.strip().split(",")]

print(program)

index = 0  # pointer 

for v in program[index]:
  #do something
Is it correct?
not exactly. v would be only one integer value.
and before as explained in the text. It´s important to read the text carefully always. :-)

Here´s my function which can be used for both parts of the day.