Python Forum
item = index position - list of list
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
Reply
#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.
Reply
#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?
Reply
#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.
Reply
#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.
Reply
#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
Reply
#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
Reply
#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.
Reply
#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?
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,091 Sep-24-2023, 05:03 AM
Last Post: deanhystad
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,466 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,738 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Replace for loop to search index position illmattic 5 1,228 Sep-03-2022, 04:04 PM
Last Post: illmattic
Question Keyword to build list from list of objects? pfdjhfuys 3 1,500 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  IndexError: list index out of range dolac 4 1,846 Jul-25-2022, 03:42 PM
Last Post: deanhystad
Question Finding string in list item jesse68 8 1,801 Jun-30-2022, 08:27 AM
Last Post: Gribouillis
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,603 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020