Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

How do I create a list of ranges in Python?

I need to create a list of ranges in Python. The list will contain 100 values over range(100,5000,100). Each element of the list needs to contain 100 values. For example, L[0] will be 0 to 100 in increments of 1, 0 - 200 will be in increments of 2, 0 - 5000 will be in increments of 50. I have the following code:

K = list(range(100,5000,100))
    L = []
    for i in K:
        Q = list(range(100, i, int(i/100)))
        L.append(Q)

This produces L with elements that are not all of length 100 and not the desired sequences. I can't seem to figure this out. Please help! Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This would be a possible solution:

n = 100
L = []
for i in range(1, n + 1):
    l = list(range(0, i * n, i))
    L.append(l)

Inspecting the list yields:

L[0]
>> [0, 1, 2, 3, ..., 100]

L[1]
>> [0, 2, 4, 6, ..., 200]

len(L)
>> 100

len(l)
>> 100

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...