0

Given a range of numbers from 1 to 81, I want to make 9 lists from range 1 to 81, of 9 lists.

The 1st list would be [1,2,3,4,5,6,7,8,9]. The 2nd list would be [10,11,12,13,14,15,16,17,18], and so on for 9 lists.

Then, I want to be able to print any of the 9 lists of numbers, so the lists would need to be labelled somehow. Perhaps like this:

list 0 = 0[[1,2,3,4,5,6,7,8,9]]. list 1 = 1[[10,11,12,13,14,15,16,17,18]], and so on for 9 lists. Then I can enter a command to print(list[0:]), and the program will print [1,2,3,4,5,6,7,8,9].

I want the program to make the lists, and not to have to make them manually.

steven-14
  • 1
  • 2
  • I would suggest: 1) checking how to divide a list (i.e. numbers 1 to 81) into chunks, 2) storing the chunks in a dictionary using keys 0, 1, 2, ...8 allowing access to any of the nine chunks. – DarrylG Oct 05 '22 at 20:25
  • https://stackoverflow.com/questions/2130016/splitting-a-list-into-n-parts-of-approximately-equal-length – Andrew Ryan Oct 05 '22 at 20:25
  • Try: `lists = {k:[i for i in range(9*k+1, 9*k+10)] for k in range(9)}` to generate lists. Then to print one, such as the 2-nd (0-indexed) use `print(lists[1])` – DarrylG Oct 05 '22 at 20:33

1 Answers1

0
def seprange(i,j,x):
    j+=1


    while i<j:

        yield [i for i in range(i,i+x)]
        i+=x
    
d={}      

for k,l in enumerate(seprange(1,81,9)):
    d[k]=l
print(d[1])
print(d[0])
islam abdelmoumen
  • 662
  • 1
  • 3
  • 9
  • In general, `[i for i in ...]` is more neatly spelled `list(...)`. Here, it makes the code harder to understand, because the `i` of the list comprehension conflicts with the `i` parameter. Also, it is not useful to `print` some of the lists locally and then throw away the `d` value without `return`ing it. – Karl Knechtel Oct 05 '22 at 20:34
  • 1
    That's what I wanted thanks. Any hints on how I can learn to do this type of thing myself? Currently I am on chapter 9 of python for everybody by Chuck Severance. – steven-14 Oct 05 '22 at 22:01
  • Learn the basics well and then try to solve problems from the easiest to the most difficult. Try reading some codes and benefiting from them and imitating them – islam abdelmoumen Oct 05 '22 at 22:36