-3

I'm doing a project for myself which involves raspberry pico and an oled display. I'm using micropython but I think this is more like a generic python question.

I need to populate the screen with images, the fastest way to do it is to ask the pico to turn on or off every pixel.

I'm super stuck in the project since I know how to transform a bitmap image into a 0/1 grid but the result I have is a string like this:

a = "11111001010101010, 11111001010101010, 11111001010101010, 11111001010101010"

in order for the screen to work I need to build a matrix like this:

b = [
    [ 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [ 0, 1, 1, 0, 0, 0, 1, 1, 0],
    [ 1, 1, 1, 1, 0, 1, 1, 1, 1],
    [ 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [ 1, 1, 1, 1, 1, 1, 1, 1, 1],
    [ 0, 1, 1, 1, 1, 1, 1, 1, 0],
    [ 0, 0, 1, 1, 1, 1, 1, 0, 0],
    [ 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [ 0, 0, 0, 0, 1, 0, 0, 0, 0],
]

each array is a line on the screen, 0 for black pixel and 1 for white pixel.

What is the fastest way to transform a string containing rows separated by commas into a matrix like the one above? I spent 10 hours on this, I think it's time to surrender :) Thank you in advance for your answers

taccola
  • 3
  • 1
  • 3
    Welcome to SO! "I spent 10 hours on this, I think it's time to surrender :)" -- normally, we request that you share your attempt(s). Where are you stuck, specifically? Thanks. Also, your output is a 9x9 matrix but the input string is a 17x4 matrix. Can you clarify exactly how the 9x9 matrix was built from this string? – ggorlen Aug 31 '21 at 16:34

2 Answers2

1

You can use list comprehension:

[list(map(int, word)) for word in a.split(", ")]

If there is no guarantee that commas are followed by exactly one space, then you need to split by comma only, and then strip spaces:

[list(map(int, word.strip())) for word in a.split(",")]
trincot
  • 317,000
  • 35
  • 244
  • 286
  • I already tried this method but I keep having the same error... this is my code ``` a = "10001,01100,100010,10010" b = [list(map(int, word)) for word in a.split(", ")] print(b) ``` and the error is: invalid literal for int() with base 10: ',' – taccola Aug 31 '21 at 16:59
  • The error you mention occurs because some commas are not followed by spaces. Is the number of spaces variable? Can you edit your question and describe the rules of the input format? – trincot Aug 31 '21 at 17:04
  • See addition to answer to deal with variable number of spaces. – trincot Aug 31 '21 at 17:06
  • 1
    Sorry, it was my fault because I didn't notice that in your code the commas were followed by a space and in my list they were not. Thank you so much! – taccola Aug 31 '21 at 17:14
-3

First split the big string by a split character , in this case

Then map each resultant token converting it to a char array

    String a = "11111001010101010, 11111001010101010, 11111001010101010, 11111001010101010";
    String[] b = a.split(",");
    char[][] result = new char[b.length]["11111001010101010".length];
    int i = 0;
    for (String c : b) {
        result[i++] = c.trim().toCharArray(); 
    }
    for (char[] d : result) {
        System.out.println(d);
    }

Using python

str = "111111111110000,111111111110000"
list = str.split (",")
for x in str:
  to_array = [char for char in str]
  print(to_array)

Instead of printing the result just add it to a array, so you'll end up with a matrix from the string

Francisco Valle
  • 613
  • 10
  • 10
  • 3
    This is not a Java question. – trincot Aug 31 '21 at 16:46
  • String, trim and split methods exists in python, so you can take it as a starting point. – Francisco Valle Aug 31 '21 at 16:47
  • Take a look at this, it may help, https://www.codegrepper.com/code-examples/python/frameworks/django/convert+comma+separated+string+to+array+python and https://www.studytonight.com/python-howtos/how-to-convert-string-to-character-array-in-python – Francisco Valle Aug 31 '21 at 16:52
  • 1
    Java code does not belong in an answer to a strictly Python question, and even your Python code is incorrect. Variables should not be named after builtins, otherwise you shadow their function that you might need later. And most importantly, your Python code is simply wrong - it won't do what the OP wants to do. Friendly suggestion: test your code before rushing to be the first answer on a question. All you end up doing is losing rep and looking dumb. – MattDMo Aug 31 '21 at 17:22
  • 1
    @FranciscoValle - "string, trim and split methods exist in python.." You're like a baseball player that showed up to football practice with a bat and a glove, claiming it's legit because both games have a ball. – OneMadGypsy Sep 01 '21 at 05:05