Question about Python for statement.

Asked 2 years ago, Updated 2 years ago, 14 views

list =list(range(0, 20))
a = []
for i in w:
    if i >= 7*0 and i < 7*1:
        a.append(0)
    elif i >= 7*1 and i < 7*2:
        a.append(1)
    .
    .
    .
    .
    elif i >= 7*n and i < 7*(n+1):
        a.append(n)
    else:
        a.append(n+1)

If n is small in the for statement above, you can manually enter elif one by one.

As the length of the list gets longer, it is difficult to enter manually if n increases

I wonder what to do with a simple for statement.

Thank you for your reply.

python

2022-09-20 17:37

1 Answers

a.append(min(i//7, n+1))


2022-09-20 17:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.