1

First time posting. Really new to python.

I currently have an LED panel that's 16x96 with uv, green, and blue lights. The idea is to make this entire panel (a matrix) light up with a specific color selected by the user at a specific intensity entered by the user. I know there is a much more efficient way to write the code with variables but I need a little help. How can I light up the first column, fourth column, seventh column, and so on until the entire LED panel is lit up to UV? Or in case of green: the second column, fifth column, eigth, etc.? Basically +3 from the original each time. The code I have written below:

    panel = np.zeros((16,96))

    #function that encompasses object color when selected
    def click1():
        global a
        global i
        i = lightintensityentry.get()
        a = var1.get() #variable associated with selection
        if a == 1:
            panel[:,0::3] = i
            print(panel)
            print("Object is a UV light!")
        elif a == 2:
            panel[:,1::4]= i
            print(panel)
            print("Object is a green light!")
        elif a == 3:
            panel[:,2::3]= i
            print(panel)
            print("Object is a blue light!")
        elif a == 4:
            print(panel) #would be all zeros, therefore no light
            print("Object produces no light!")

I thought something similar to this would help: Edit every other item in an array It did not produce what I expected. Any help would be appreciated.

Looking for an explanation of what this does as well: panel[:,0::3] What does the 0::3 do exactly?

Thank you!

1 Answers1

0

0::3 is a standard Python slicing notation. It is equivalent to a sequence of indexes, starting from 0 and increasing by step = 3.

In numpy slicing options differs from ones used in Python lists as it can be noticed in your example.

  1. It allows to manipulate multi-dimensional arrays in a more flexible way. For example, if it is placed after comma like in panel[:,0::3], it means that copy of columns of this 2D-array is taken.
  2. panel[:,0::3] = i is assignment that is elementwise.

Note that panel[:,0::3] = i doesn't assign elements to a copy of array, it selects specific items in original one and replaces them instead. Both of these concepts won't work using Python lists.

mathfux
  • 5,759
  • 1
  • 14
  • 34