1

Lets say I have this List of Lists:

x=[['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0']]

now I get the input of

(2,2), (5,4)) (second coordinate represents last pixel where no "1" should be)

which results in 

x=[['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0'],
 ['0', '0', '1', '1', '1', '0'],
 ['0', '0', '1', '1', '1', '0']]

for index,i in enumerate(x[3][2:4]):
    i ="1"

I dont know why this doesn't work for the first line...

any ideas how I could make the rectangle in one go are also welcome :) thanks in advance

(0,0), (1,1)
should give 

x=[['1', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0'],
 ['0', '0', '0', '0', '0', '0']]
Orsons
  • 61
  • 5

2 Answers2

1

let's start from why it only works on the second line as you stated.

for index, i in enumerate(x[3][2:4]):
    i = "1"

Here you are accessing x[3] which is the third row of your list of lists. So what you are actually changing is a bunch of columns (2:4) from that single row.

Let's see how to do it in one go correctly

x = [['0', '0', '0', '0', '0', '0'],
    ['0', '0', '0', '0', '0', '0'],
    ['0', '0', '0', '0', '0', '0'],
    ['0', '0', '0', '0', '0', '0']]

start = (2, 2)
end = (5, 4)

for i in range(start[0], end[0] - 1):
    x[i][start[1]:end[1] + 1] = ['1'] * (end[1] - start[1] + 1)

x value after the changes:

x: [['0', '0', '0', '0', '0', '0'],
    ['0', '0', '0', '0', '0', '0'],
    ['0', '0', '1', '1', '1', '0'],
    ['0', '0', '1', '1', '1', '0']]

We iterate of the rows of the list, each x[i] will be a list as well, we change its values (using slicing) to the appropriate value. The appropriate value must be of the same type of the list slice so that's why we create another list['1'] * (end[1] - start[1] + 1) of the length we need.

JunkyByte
  • 106
  • 3
  • 1
    There is a Proble with start=(0,0) and end=(1,1) it should turn the upper left corner into a 1 but it stays 0 – Orsons Nov 20 '20 at 15:09
  • 1
    Hello, the way you defined the coordinates is a bit weird to me. Let me think about it for a couple minutes will get back to you – JunkyByte Nov 20 '20 at 15:15
  • yeah I know the coordinates are a brainf*** – Orsons Nov 20 '20 at 15:21
  • 2
    I think it would be easier also for you if we represent start position and end position both inclusive so that (0, 0) -> (0, 0) draws a 1 top left. The conversion between old and new ones is easy as you just need to add a (2, 0). If you agree on this I will update the answer with correct code. Edit: Another thing you may enjoy this way is that your coordinates will be in the bounds of the list of lists sizes (right now they are not) – JunkyByte Nov 20 '20 at 15:24
  • 1
    that's not really going to help me as this is only one method of a Class... – Orsons Nov 20 '20 at 15:27
0

Note: I don't realize that the element of the list is a string. Please change [0] to ['0'].

This is my humble answer. This code:

x, y = 6, 4
start, end = (0, 0), (1, 1)
dat = []
for i in range(y):
    if start[1]<=i<end[1]:
        dat.append([0]*(start[0])+[1]*(end[0]-start[0])+[0]*(x-end[0]))
    else:
        dat.append([0]*x)
data

or this code:

x = [[0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0]]
start, end = (0, 0), (1, 1)
for i in range(len(x)):
    if start[1]<=i<end[1]:
        x[i][start[0]:end[0]] = [1] * (end[0] - start[0])
x

will results:

[[1, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0, 0]]
Sandi
  • 148
  • 7