0

I want to take step value in a for loop from an array. some thing like,

step = [0,1,2,15,3,87] #values in no order  
for i in range(0, len(raw_pkt), step):

The step value should be updated in each iteration. Is there any workaround for that? I know that range() expects an integer value not a list. But I want to take values from the list on each iteration.

3
  • No, in that case you better use a while loop. A range(..) is constructed once. Commented Oct 25, 2017 at 10:02
  • @WillemVanOnsem Requesting your dupe hammer services... ^ Commented Oct 25, 2017 at 10:05
  • @DavidG I can't, I added the tag. Commented Oct 25, 2017 at 10:10

4 Answers 4

1

Iterating through step:

If you think about what you are trying to achieve, you don't really care about the list that you are trying to index as the step values are fixed. So even if the length of the raw_pkt was a million, because your step list is only length 6, you will only ever have 6 indexes.

Once you realise this, the code becomes pretty simple, you just want to iterate over the step values and increment a variable. This variable can be used to index a list or whatever, but this doesn't matter.

This makes the code pretty short and concise:

step = [0, 1, 2, 15, 3, 87]
i = 0
for s in step:
   i += s
   #code...

so to demonstrate that this works, we can do a more easy-to-follow example:

step = [0, 1, 2, 4, 1]
l = [5, 6, 3, 6, 2, 7, 3, 1, 8]
i = 0
for s in step:
   i += s
   print(i, l[i])

which outputs:

0 5
1 6
3 6
7 1
8 8

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

I guess what you want ist itertools.accumulate.

from itertools import accumulate

for i in accumulate([0,1,2,15,3,87]):
    print(i)

# would print 0 1 3 18 21 108

2 Comments

I don't think that was the question. i think the question was how to iterate over "raw_pkt" with steps defined in another list. - I might be wrong tho
@EvilSmurf I guess this is exactly what that question is about. The question only concerns the range creation, not the indexing of another array.
1

there's an easy solution to this:

for pkt in [raw_pkt[i] for i in step]:
    print pkt

with

step = [0,1,2,15,3,87]

and

raw_pkt = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199]

the result would be

100
101
102
115
103
187

5 Comments

Technically, this doesn't do what the OP wants, it seems as if they want the indexes, not the values from the raw_pkt list...
Also, you can change the list-comprehension: [raw_pkt[i] for i in step] to a generator which is more efficient. i.e (raw_pkt[i] for i in step).
the indexes are already in step - unless step contains values for which you want the index in raw_pkt in which case simply change the foor loop from for pkt in [raw_pkt[i] for i in step] to for pkt_idx in [raw_pkt.index(i) for i in step]
the way I understand the question, these are the step values between the indexes, so a normal range such as for i in range(10) would have a step list of [1, 1, 1, ...]
possible. that's where OP has to get more specific. if that is what (s)he wants, then I also agree with your answer.
1
step = [0,1,2,15,3,87] #values in no order for i in range(0, len(raw_pkt), step):

laststp = 0

for stp in step:
   for i in range(stp-laststp, len(raw_pkt), stp):
      #do something
      laststp = stp
      break #this should make sure that each step is applied once.

2 Comments

Have you forgotten to define laststp at the start of the for-loop? Because this won't run at the moment as laststp is not defined (as well as raw_pkt but I assume you are leaving that up to the OP)
Yes you're right

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.