Python enumarate with for loop

I want to turn table1 into table2 using value of table1 into table2’s row number and make them into each lists. So far I was able to make each lists with enumarate() but can’t figure out how to use that in the for loop. I attached my file thanks in advance.

table1
0
1
1
1
2
2
3
3
3
4
4
5
5
5
5

table2
0
1 2 3
4 5 6 7 8
9 10
11 12 13 14

This is where I am stuck… if I change j==0, j==1, j==2 and so on it creates each rows fine but how do I make it to each index of for loop? When I tried j==x it returns empty lists.

for x in range(primCount):
	boo = [i for i, j in enumerate(listPts) if j == '1']
	print(x, boo)
	t2[1,0] = boo

primTable.10.toe (4.1 KB)

Hi @akiko_yamashita,

i skipped the numpy and hope that’s ok - my solution is just using the python set which collapses a list into a set that only contains each item uniquely.
After using table.findCells() will return all rows that contain that value.

tbl1 = op('table1')
tbl2 = op('table2')

tbl2.clear()

# collect all numbers from table1 and turn it into a set
indices = set([int(x.val) for x in tbl1.col(0)])

# number of rows required is the max value in the set (+1)
maxIndex = max(indices)
maxIndex += 1
tbl2.setSize(maxIndex,1)

# loop through the set and find it's value in the table
for c in indices:
	rows = [x.row for x in tbl1.findCells(c)]
	tbl2[c,0] = rows

Just to fully answer your question:
the condition in the list comprehension compares strings: j == '1' - so x needs to become a string as well:

boo = [i for i, j in enumerate(listPts) if j == str(x)]

This is as a cell’s value is always interpreted as a string if retrieved by cell.val

Hope this helps
Markus

1 Like