0

Below, set1, set2, set3 are lists with len(setn) =len(index). I want to loop over each of these lists (setn) as follows,

index = range(10)
set1 = range(10,20)
set2 = range(30,40)
set3 = range(40,50)
listset = [set1, set2, set3]
for i in listset:
  for k, j in zip(index, i):
     print k, j

Result:
0 s
1 e
2 t
3 1
0 s
1 e
2 t
3 2
0 s
1 e
2 t
3 3

How can I get a result that prints the each element of "index, set1" (as given below),followed by "index, set2", followed by "index, set3".

0 10
1 11
2 12
3 13
4 14
5 15
6 16
7 17
8 18
9 19
and so on...
10
  • your for loop works to get the expected result, what are you searching for? Commented Oct 11, 2016 at 19:38
  • 2
    Sorry but I think that my editing of the question solved your problem... So it was just a typo with the quotes. Commented Oct 11, 2016 at 19:38
  • The correctly edited post (replacing the literals 'set1'etc with the real variable names set1 (note no more apostrophes, produces the wished output ;-) Commented Oct 11, 2016 at 19:39
  • For a moment I thought I had destroyed the question :) Commented Oct 11, 2016 at 19:39
  • @Jean-FrançoisFabre no very compact inline answer ;-) Commented Oct 11, 2016 at 19:40

2 Answers 2

1

You want to combine enumerate and itertools.chain

from itertools import chain
s1 = range(10)
s2 = range(10, 20)
s2 = range(20, 30)
c = chain(enumerate(s1), enumerate(s2), enumerate(s3))
for i, n in c:
    print(str(i).ljust(4), n)
Sign up to request clarification or add additional context in comments.

3 Comments

Could you please give a brief explanation of how this chain works. For example, I tried "for i in c: print i", which outputs the same results with a different formatting. So what is the difference between print i, n and simply print i. How do I get print only the first (or any specific) set (s1, s2) from c?
@gogo We give chain an arbitrary number of iterators. chain returns a generator that yields values from those iterators in order to exhaustion. So we first get all the things from s1 then from s2 and so on. If you only want s1 and s2 from c then you have to either check every value to see if it is the first value of s3 or exclude s3 from the definition of c
@gogo If you look at the output of list(enumerate([10, 11, 12])), you'll see that it produces [(0, 10), (1, 11), (2, 12)]. What enumerate does is replace each element in a sequence with a tuple of (index, element) where index is the index of that element in the list, So when you write for i in c:, i is equal to those tuples. When you write for i, n in c:, it automatically unpacks the tuples so that i is the index and n is the element .
1

You can concatenate set1, 2 and 3 together, then use itertools.cycle(index) and zip the resulting two things together: zip(itertools.cycle(index), set1 + set2 + set3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.