0

I was wondering if it is possible to print a value that is inside a list of lists of tuples if you already know the indexes path.

1. List[i][j]
2.   list2[x][y]
3.      list3[z][w]
4.         etc.

I would like to do something like this:

str( list[i][0 [0][2 [0][0]] )

To get the value at indexes 0,0 in the tuple at indexes 0,2 of a list at indexes i,0 of the main list.

Edit: Sorry, I'm learning. Console returns this:

    [Record(items=set({'item1','item2'}),
            id=6439,
            stats=[OrderedStatistic(items_b=set({'item2'}),
                                    items_a=set({'item1'}),
                                    row=123,
                                    col=321)]),
     Record(... 

I'm looking for the "row" value. I didn't know it had a name, it wasn't showed in my viewer.

Edit 2:

Improvement. It works, but I'm using a temporary variable because oldList[0].stats[0].row returns a 'stats' object has no attribute row.

for i in range(0, len(oldList)):
    tmpList = oldList[i][2]
    newList.append('ID:\t' + str(oldList[i][1])+ '\nROW:\t' + str(tmpList[0][2])

or

for i in range(0, len(oldList)):
    tmpList = oldList[i][2]
    newList.append('ID:\t' + str(oldList[i][1])+ '\nROW:\t' + str(tmpList[0].row)
Falk
  • 11
  • 2

2 Answers2

0

The syntax in this case would be as follows:

print(lst[i][0][0][2][0][0])

That nested structure is more than just a list of lists of tuples though. If that were the case, then the following minimal example list could have the 'a' referenced with a simple lst[0][0][0]:

lst = [[('a', 'b', 'c'), ('d', 'e', 'f')]]
  • Thank you Brandon. `lst = [[('a', 'b', 'c'), ('d', ('e1' , 'e2'), 'f')]]` It should look like this, with e2 as the value I'm looking for. I also updated my original post with a sample. – Falk May 07 '18 at 17:11
  • In that case, you would want `lst[0][1][1][1]`. – Brandon Krueger May 07 '18 at 17:28
  • Yes, I already tried but it returns a `TypeError "set" object does not support indexing` That was my first attempt. I made a test on an array and it works. I'm probably misreading the lists. – Falk May 07 '18 at 17:40
  • For your example, try `lst[0].stats[0].row`. – Brandon Krueger May 07 '18 at 17:52
  • It doesn't work, but you helped me a lot! I edited my post to make it a bit more readable. – Falk May 07 '18 at 19:33
0

In a simple way for the lst = [[('a', 'b', 'c'), ('d', ('e1' , 'e2'), 'f')]] example you can use lst[0][1][1][1] to reach e2, but I imagine that you have the indexes in an array like ind_array. in this case you may have to do something like this:

ind_array = [0, 1, 1, 1]
desired_value = lst
for i in o :
    try:
        desired_value = desired_value[i]
     except IndexError:
         print('index not found.')
         break
 print(desired_value)

Let me know if you have problem with answer or you will have the indexes differently.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59