Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 item = index position - list of list
#1
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
Quote
#2
Don’t use list and sum as names.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#3
(Dec-02-2019, 03:13 PM)perfringo Wrote: Don’t use list and sum as names.

I don't understand, can you explain better?
Quote
#4
No built-in list() and sum() anymore....
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#5
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.
Quote
#6
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
Quote
#7
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
Quote
#8
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.
Quote
#9
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?
Quote
#10
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to compare a list to a list of lists kevthew 1 119 Dec-22-2019, 11:43 AM
Last Post: ibreeden
  How to find something in a list using its index rix 1 99 Dec-20-2019, 04:12 PM
Last Post: stullis
  Add list item into executable command zhome888 2 173 Dec-19-2019, 12:23 AM
Last Post: zhome888
  Randomly assign values in List 1 to a value in List 2 Seneca260 7 572 Dec-16-2019, 12:57 PM
Last Post: BamBi25
  extract first and last 5 elements from given list and generate a new list. Raj_Kumar 1 120 Dec-07-2019, 05:03 PM
Last Post: ichabod801
  List Comprehension - Creating a list of the length of an item help paul41 2 200 Nov-18-2019, 10:21 AM
Last Post: perfringo
  IndexError: List index out of range issue Adem 1 479 Nov-01-2019, 10:47 PM
Last Post: ichabod801
  IndexError: list index out of range bug? CaptainCsaba 4 813 Oct-18-2019, 12:38 PM
Last Post: CaptainCsaba
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 227 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 186 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu

Forum Jump:


Users browsing this thread: 1 Guest(s)