Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...