1

I have a list of integers and a QTableWidget populated with 33 rows.

My list is like this (which corresponds to the rows in the QTableWidget):

my_list = [15, 14, 1, 2, 1]

I would like to extract information from the table by the number of rows.

For example, I want to loop through the table for the first 15 rows, then the next 14 rows, then the next 1 row etc. So at the moment, I'm using:

my_list = [15, 14, 1, 2, 1]

for row in range(15):
    # Do something

for row in range(15,29)
    # Do something

for row in range(29,30)
    # Do something

...

As the values in the list (and the number of items) can change, what is the most efficient way of doing this?

Joseph
  • 586
  • 1
  • 13
  • 32

2 Answers2

1

This way you can get your pairs an use them for range():

import numpy as np

my_list = [15, 14, 1, 2, 1]

top = list(np.cumsum(my_list))
bottom = list(np.subtract(np.cumsum(my_list),my_list))

pairs = zip(bottom,top)
ranges = iter([range(x[0], x[1]) for x in pairs])

Now, you can just go:

for item in next(ranges):

EDIT #1

If your # Do something is always different you can use:

for row in next(ranges):
    # Do something

for row in next(ranges):
    # Do something

EDIT #2

If you your #Do something is always the same you can go:

for item in my_list:
    for row in next(ranges):
        # Do something
zipa
  • 27,316
  • 6
  • 40
  • 58
  • When I use `for item in next(ranges):`, it only does this for the first 15 items. Am I supposed to use something else? – Joseph Apr 03 '17 at 12:02
  • See the edit. I understood you are changing the operation for each range. – zipa Apr 03 '17 at 12:14
  • Ahh I see what you mean, thanks! In the question, I mentioned 5 items in the list so I would have to use `for row in next(ranges):` 5 times. If I have 3 items in the list, I would have to use `for row in next(ranges):` 3 times. Basically I am running a GIS plugin and so the number of items in the list can vary. Is there a method to accomodate this change without me having to _hardcode_ `for row in next(ranges):` x times? I.e. can it be done in a single loop? – Joseph Apr 03 '17 at 12:20
  • Made another edit in case your `# Do something` is always the same. – zipa Apr 03 '17 at 12:37
  • Yes, I was about to make a comment about using a nested `for` loop as described in [Python - Iterating through list of list](http://stackoverflow.com/questions/6340351/python-iterating-through-list-of-list)). Now it works perfectly, many thanks :) – Joseph Apr 03 '17 at 12:38
1

You can try it by using index of lists

for row in range (my_list[0]):
    print "abc"
for row in range (my_list[1]):
    print "bc"
for row in range (my_list[2]):
    print "ac"
for row in range (my_list[3]):
    print "amc"
for row in range (my_list[4]):
    print "c"
anonymous
  • 143
  • 1
  • 2
  • 16
  • Thanks for your answer but the idea is that the number of items in the list can change (i.e. I have shown `5` but it could also be `3`). I'm looking for a more generic way instead of _hardcoding_ it :) – Joseph Apr 03 '17 at 12:13