Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Help with 2d Array Error

newb75

New Coder
I am trying to create a 2d array consisting of various attributes from an object, then sorting the array to find a specific min and its related values. I keep getting the error: list assignment index out of range

Here is some sample code:
while c < len(cool_stuff):
if cool_stuff[c].coolest_stuff = 0:
a_list = []
b_list = []
c_list = []
d_list = []
a_list.extend([cool_stuff[c].a_1,cool_stuff[c].a_2,cool_stuff.a_3,cool_stuff.a_4])

b_list.extend([cool_stuff[c].b_1,cool_stuff[c].b_2,cool_stuff[c].b_3,cool_stuff.b_4)


and so forth. Just assume proper indents.
Next is
col = int(len(d_list))
silly_array = [[0] * col for j in range(4)]
for d in range(len(d_list)):
silly_array[0][d] = int(a_list[d])

I keep getting an error message doing this. What is the problem.
 
Last edited:
I am trying to create a 2d array consisting of various attributes from an object, then sorting the array to find a specific min and its related values. I keep getting the error: list assignment index out of range

Here is some sample code:

while c < len(cool_stuff):
if cool_stuff[c].coolest_stuff = 0:
a_list = []
b_list = []
c_list = []
d_list = []
a_list.extend([cool_stuff[c].a_1,cool_stuff[c].a_2,cool_stuff.a_3,cool_stuff.a_4])

b_list.extend([cool_stuff[c].b_1,cool_stuff[c].b_2,cool_stuff[c].b_3,cool_stuff.b_4)


and so forth. Just assume proper indents.
Next is
col = int(len(d_list))
silly_array = [[0] * col for j in range(4)]
for d in range(len(d_list)):
silly_array[0][d] = int(a_list[d])


I keep getting an error message doing this. What is the problem.
Python:
import random
    
# dummy data
class Thing:
    def __init__(self, id_val):
        self.coolest_stuff = 0 # Set to 0 so the if-statement passes
        # Random values to simulate data
        self.a_1 = random.randint(1, 100)
        self.a_2 = random.randint(1, 100)
        self.b_1 = random.randint(1, 100)
        self.b_2 = random.randint(1, 100)

# simulating 'cool_stuff'
cool_stuff = [Thing(i) for i in range(5)]

# Initialize the main 2D array
silly_array = []

# Iterate through objects
for obj in cool_stuff:
    # Use '==' for comparison, not '='
    if obj.coolest_stuff == 0:
        
        # Build the row directly.         
        # This keeps related data together (a_1 belongs with b_1)
        # and ensures the list index never goes out of range.
        row = [
            obj.a_1,
            obj.a_2,
            obj.b_1,
            obj.b_2
        ]
        
        # Add the completed row to the main array
        silly_array.append(row)

# Output the raw array
print("--- The 2D Array ---")
for row in silly_array:
    print(row)

# --- SORTING & FINDING MIN ---

# For example sort by the first item in the row (Index 0, representing a_1)
# lambda x: x[0] tells Python to look at the first number in each row
silly_array.sort(key=lambda x: x[0])

print("\n--- Sorted by First Column (Min to Max) ---")
for row in silly_array:
    print(row)

# The row with the minimum value in the first column is now at index 0
min_row = silly_array[0]
print(f"\nMin Value: {min_row[0]}")
print(f"Related Values: {min_row[1:]}")
 
Back
Top Bottom