如何自动生成1-81的9个分块list并按需打印指定list
Generate 9 Consecutive Number Lists (1-81)
Here's a straightforward Python implementation to automatically create 9 lists, each containing 9 consecutive numbers from 1 to 81. You can access any list using its index (starting from 0) just like print(number_lists[0]).
# Initialize an empty list to store all sublists number_lists = [] # Loop to generate each of the 9 sublists for idx in range(9): # Calculate the starting number for the current sublist start_num = idx * 9 + 1 # Create the sublist with 9 consecutive numbers current_list = list(range(start_num, start_num + 9)) # Add the sublist to our main list number_lists.append(current_list) # Example usage print(number_lists[0]) # Output: [1, 2, 3, 4, 5, 6, 7, 8, 9] print(number_lists[1]) # Output: [10, 11, 12, 13, 14, 15, 16, 17, 18] print(number_lists[8]) # Output: [73, 74, 75, 76, 77, 78, 79, 80, 81]
Key Details:
- The main list
number_listsacts as a container for all 9 sublists, no manual creation of individual lists needed. - For each iteration in the loop:
start_numcalculates the first number of each sublist (1, 10, 19... up to 73).range(start_num, start_num +9)generates exactly 9 consecutive integers, converted to a list.
- Access any sublist using 0-based indexing:
number_lists[2]gives the third list,number_lists[5]gives the sixth, etc.
内容的提问来源于stack exchange,提问作者steven-14
相关产品推荐
相关产品推荐

