Home > Mobile >  Python increment in a list
Python increment in a list

Time:01-27

i have this code below :

list = [254,255,256]

# Getting length of list using len() function
length = len(list)
i = 0
counter = 0

while i < length:
  
  url = "https://test/api/v1/implantacao/projeto/{}/tarefa?start={}&limit=50".format(list[i], counter) 
  i  = 1
  counter  = 1
  print(url) 

Output :

https://test/api/v1/implantacao/projeto/254/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=2&limit=50

But i want this output, with the last number (counter) 0 to 3 to every item on the list :

 https://test/api/v1/implantacao/projeto/254/tarefa?start=0&limit=50 
 https://test/api/v1/implantacao/projeto/254/tarefa?start=1&limit=50
 https://test/api/v1/implantacao/projeto/254/tarefa?start=2&limit=50
 https://test/api/v1/implantacao/projeto/254/tarefa?start=3&limit=50
 https://test/api/v1/implantacao/projeto/255/tarefa?start=0&limit=50
 https://test/api/v1/implantacao/projeto/255/tarefa?start=1&limit=50
 https://test/api/v1/implantacao/projeto/255/tarefa?start=2&limit=50
 https://test/api/v1/implantacao/projeto/255/tarefa?start=3&limit=50 

How can i get this result ?

CodePudding user response:

Compute a product of two individual lists, [254, 255] and [0,1,2,3]. Then you can simply iterate over the pairs in the product with one loop.

from itertools import product


template = "https://test/api/v1/implantacao/projeto/{}/tarefa?start={}&limit=50"

for project, start in product([254,255], [0,1,2,3]):
    url = template.format(project, counter)

CodePudding user response:

Instead of keeping two counters, you can use for loops like this:

urlformat = "https://test/api/v1/implantacao/projeto/{}/tarefa?start={}&limit=50"

items = [254,255,256]

for item in items:
    for j in range(4):
        url = urlformat.format(item, j) 
        print(url)

Result:

https://test/api/v1/implantacao/projeto/254/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=3&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=3&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=3&limit=50

CodePudding user response:

lists = [254,255,256]

# Getting length of list using len() function
length = len(lists)


for li in range(length):
    for count in range(4):
        url = "https://test/api/v1/implantacao/projeto/{}/tarefa?start={}&limit=50".format(lists[li], count)
        print(url) 

result:

https://test/api/v1/implantacao/projeto/254/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=3&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=3&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=3&limit=50

and never use built-in as variable please

CodePudding user response:

Try this:

    for item in list:
        for i in range(4):
            url = f"https://test/api/v1/implantacao/projeto/{item}/tarefa?start= 
            {i}&limit=50"
            print(url)

using f string is much better than using .format() str method in most cases i recommend you read about it in python documentation. Alas the above code will give you the result you want.

CodePudding user response:

One possible approach would be, as @chepner suggested, computing the cartesian product of two lists; namely [254, 255, 256] and [0,1,2,3] to obtain:

[(254, 0), (254, 1), (254, 2), (254, 3), (255, 0), (255, 1), (255, 2), (255, 3), (256, 0), (256, 1), (256, 2), (256, 3)]

Then, map the anonymous function:

lambda xy: template.format(xy[0], xy[1]),

over that list of pairs to produce the desired output, and join the list using the newline character ("\n").

def main():
    template = "https://test/api/v1/implantacao/projeto/{}/tarefa?start={}&limit=50"
    xs = [254, 255, 256]
    ys = [0, 1, 2, 3]
    cartesianProduct = [(x, y) for x in xs for y in ys]
    print(
        "\n".join(map(
            lambda xy: template.format(xy[0], xy[1]),
            cartesianProduct
        ))
    )

if __name__ == '__main__':
    main()

Output:

https://test/api/v1/implantacao/projeto/254/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/254/tarefa?start=3&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/255/tarefa?start=3&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=0&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=1&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=2&limit=50
https://test/api/v1/implantacao/projeto/256/tarefa?start=3&limit=50
  • Related