Loop For with Zip(), how to select the amount of records?

Asked

Viewed 92 times

0

I have this code that works normally

    def pdd(number, entrada):
        for i, j, k, l, m in zip(entrada[:-1], entrada[1:], entrada[2:], entrada[3:], entrada[4:]):
            if number in i:
                print([j, k, l, m])

    list_pad = [[1, 9], [1, 15], [1, 10], [1, 11], [1, 12], [1, 13], [1, 15], [1, 14], [1, 15], [1, 16], [1, 17], [1, 18]]

    pdd(15, list_pad)

Exit:

[[1, 10], [1, 11], [1, 12], [1, 13]]
[[1, 14], [1, 15], [1, 16], [1, 17]]

But instead of printing 4 records, I would like to be able to define the quantity per parameter.

Example:

pdd(15, list_pad, 6)

And you should leave:

[[1, 10], [1, 11], [1, 12], [1, 13], [1, 15], [1, 14]]

Or

pdd(15, list_pad, 2)

And get out:

[[1, 10], [1, 11]]
[[1, 14], [1, 15]]
[[1, 16], [1, 17]]
  • 1

    I could put examples of some calls of this function and what would be the expected result?

  • @Andersoncarloswoss I reformulated my post, more summarized and with examples, see if you got it now.

  • What exactly the function needs to do?

  • Basically it will analyze the entry, which in this case is a list with small lists. Then analyze the input, and when identifying an item (list) that has the number passed by the parameter number , she will print the next 4 items (lists). This code already does, but I would like to be able to define the amount by a parameter passed to the function (instead of 4)

  • And if the list that has the value is the last?

  • Then do not print, because after it there will not be the amount I set. The code already does this, it does not print if it does not meet the quantity. What I really wanted was to be able to define how many items (lists) to print, setting by parameter.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.