#๐ diagonals from 2d list into separate 2d list isnt working
33 messages ยท Page 1 of 1 (latest)
@elder apex
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
this is basically what it should be doing
this is what it actually does
the first item of the list is correct, but the consecutive ones arent
from the top, let's start by removing hardcoded numbers, the 8 in your code sample might be relevant for the particular 2d list you are looking at, but let's think about it a bit differently
two_dimensional_list_length_2 = [
[0, 1],
[2, 3]
]
two_dimensional_list_length_3 = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
two_dimensional_lis_length_5 = [
[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24],
]
just for visual clarity and understanding the problem, let's look at some example 2d lists
in the first, length 2, we want to end up with [[0], [1,2], [3]], is that right?
and in the second, length 3, we want [[0], [1,3], [2,4,6], [5,7], [8]]
we can start to see a pattern of the length of the result
for a 2x2 list, we end up with a result of length 3
3x3 = 5
5x5 = 9
so we've worked out we're gonna need to do something 2 * list_length - 1 times
let's visualise the start and end point of each diagonal, looking at the length 3 example for now
we want to start at 0, and end at 0, so our "diagonal index" is 0 and our "row index" is also 0
for the next diagonal, of index 1, we want [1, 3] - our diagonal index is 1 and our row index is still 0 for the first number, 1
for 3, our row index has increased 0 -> 1, we need the next row, and our diagonal index of 1 wouldn't be correct, [1][1] would give us the number 4, so we need to decrement our diagonal index, we want [1][0] for the number 3
we could also visualise this as column/row indexes: for the diagonal [1, 3] we need indexes: [0][1] and [1][0]
the next diagonal, [2,4,6] requires indexes [0][2], [1][1], [2][0]
hopefully we're starting to see another pattern here
as one index increases, the other decreases
!e
two_dimensional_list_length_3 = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
]
diagonals = []
list_length = len(two_dimensional_list_length_3)
for diagonal_index in range(2 * list_length - 1):
diagonal = []
for row_index in range(list_length):
if diagonal_index - row_index >= 0 and diagonal_index - row_index < list_length:
diagonal.append(two_dimensional_list_length_3[row_index][diagonal_index - row_index])
diagonals.append(diagonal)
print(diagonals)
@tranquil wedge :white_check_mark: Your 3.12 eval job has completed with return code 0.
[[0], [1, 3], [2, 4, 6], [5, 7], [8]]
add in a little check that we're not out of range for the indexes and it all comes together nicely
hopefully i did a reasonable job of explaining that and it makes sense, but if you have any questions do fire away
hey there, sorry for the late reply
let me have a look at all this
yep
oh i see
wow, thanks this works. i adjusted it to meet my criteria. the idea for a formula to figure out the length was smart, ill try to think like that next time
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.