Python two-dimensional list, putting 5050 data into a triangle shape

Asked 2 years ago, Updated 2 years ago, 35 views

If list1 has 5050 data from 1 to 5050, I'd like to convert it into a two-dimensional list.

Put 1 in (0,0) and (1,0) = 2, (1,1) = 2, (2,0) = 4, (2,1) = 5(2,2) I want to make a two-dimensional arrangement form a triangle in this way, but what should I do?

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

python list

2022-09-20 19:14

1 Answers

>>> list1 = list(range(5050))
>>> tri = []
>>> i = 1
>>> s = 0
>>> while list1[s:]:
    tri.append(list1[s:s+i])
    s = s+i
    i+=1


2022-09-20 19:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.