0

I have the following code. I want to start the for loop at the index found in the for loop. essentially, I'm slicing the list (R_sigma11, etc.) into two to search for duplicate numbers in that list and saving their indexes. I keep getting TypeError: 'int' object is not subscriptable. How can I get past this?

index = []
start = 0
bam = len(R_min_MST_list)

for n in range(bam[start:]):
     if R_min_MST_list[n] == 0:
          index.append(R_sigma11.index(R_min_MST))
     elif R_min_MST_list[n] == 1:
          index.append(R_sigma22.index(R_min_MST))
     elif R_min_MST_list[n] == 2:
          index.append(R_Tau12.index(R_min_MST))

start += index[0]
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    You need to understand "subscriptable" (accessing x of foo: foo[x]) and / or why R_min_MST_list is an int and not a "container". https://stackoverflow.com/questions/216972/what-does-it-mean-if-a-python-object-is-subscriptable-or-not – Ярослав Рахматуллин Mar 25 '22 at 06:38

2 Answers2

1

bam is an int. you can not access an index of an int.

bam[start:]

the operation above ("access start: of bam") is invalid.

0

bam is an int

You can't index a number (bam[start:])


Your code may need to look like this:

bam = len(R_min_MST_list)

# range(bam) will create a list [0,1,2,3 ...] for the length of R_min_MST_list
for n in range(bam):
     if R_min_MST_list[n] == 0:
          index.append(R_sigma11.index(R_min_MST))
     elif R_min_MST_list[n] == 1:
          index.append(R_sigma22.index(R_min_MST))
     elif R_min_MST_list[n] == 2:
          index.append(R_Tau12.index(R_min_MST))
Freddy Mcloughlan
  • 4,129
  • 1
  • 13
  • 29