简体   繁体   中英

How to run loop in between the values of list for e.g. List = [0, 417, 2050, 2221, 3039]

I want to run 1st loop from List[0] and List[1] and then 2nd Loop from List[1] and List[2] and so on

for i in range(0,len(List)):
   for z in range(data[i],data[i+1]):
        print("Hi")

If I understand the question correctly, your code almost works, but is meaningless. If you loop from index 0 to index 1, then from index 1 to index 2, it will be the same as if you loop from index 0 to index -1.

test_list = [0, 417, 2050, 2221, 3039]

test = []
test2 = []

for i in range(0, len(test_list)-1):
    for z in range(test_list[i], test_list[i+1]):
        test.append(z)
        print(f"Hi {z}")

# Will generate same result as
for i in range(test_list[0], test_list[-1]):
    test2.append(i)
    print(f"Hi {i}")

# Verify that
for z, i in zip(test, test2):
    assert z == i

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM