-1

For Excel use, I want to create a code that represents A1, A2,...D1, ... AB1, AB2, ...

field = [A, B, C, ..., AA, AB, ..., YZ, ZZ]
data = [[1, 2,3,4,5], [2, 3,4,5,6], [3, 5,4,3,5], [4, 5,6,7,7],...] #[index, data1, data2,...]

for i in field:                # A, B, C, ...
    for j in range(0,10):      # First index in data
        for k in range(1,4):
            print(str(i)+data[j][k])

The result I want is A2 A3 A4 A5 B3 B4 B5 B6 C5 C4 C3 C5 ...

I think I need to use a break statement, but how do I do that?

1
  • The problem you are encountering is that you want to pair up each element of field with the corresponding sub-list from data first (before iterating over the sub-list to make your outputs). But these nested loops mean to use every element of data, for each element of field (i.e. for 10 fields and 10 data sublists, you get 100 results instead of 10). Solving this is called iterating in parallel. Please see the linked duplicate to understand how to do it. Commented Mar 12, 2024 at 6:58

1 Answer 1

0
for i, column in enumerate(field):  # A, B, C, ...
    for j in range(len(data)):      # Iterate over rows in data
        if j < 10:  # Limit to first 10 rows if necessary
            for k in range(1, 5):   # Assuming you want to skip the first element in each row and limit to next 4
                if i < len(data[j]) - 1:  # Check if column exists in this row
                    print(f"{column}{j+2}: {data[j][k]}")
                else:
                    break  # Break if the column index exceeds the data row length
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but above result is A2: 2 A2: 3 A2: 4 A2: 5 A3: 3 A3: 4 A3: 5 A3: 6 A4: 5 A4: 4 A4: 3 A4: 5 A5: 5 A5: 6 A5: 7 A5: 7 B2: 2 B2: 3 B2: 4 B2: 5 B3: 3 B3: 4 B3: 5 B3: 6 B4: 5 B4: 4 B4: 3 B4: 5 B5: 5 B5: 6 B5: 7 B5: 7 C2: 2 C2: 3 C2: 4 C2: 5 C3: 3 C3: 4 C3: 5 C3: 6 C4: 5 C4: 4 C4: 3 C4: 5 C5: 5 C5: 6 C5: 7 C5: 7 D2: 2 D2: 3 D2: 4 D2: 5 D3: 3 D3: 4 D3: 5 D3: 6 D4: 5 D4: 4 D4: 3 D4: 5 D5: 5 D5: 6 D5: 7 D5: 7
The result I want is A2 A3 A4 A5 B3 B4 B5 B6 C5 C4 C3 C5...
This is completely wrong as it fails to understand the problem OP encountered. (Said problem is also a common duplicate.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.