average_coder25
average_coder25

Reputation: 57

How to convert 3d array as string

I need to convert a 3d array I have in Python3 into a string with a specific format. My current 3d array is below:

[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]

I need this as a string, but also want to replace any instance of 0 and make it into the string '----'. If a value is not 0, then leave it.

I tried using join: ''.join(str(e) for e in myArray) but the format did not come out as I wanted.

I expected my results to look like this:

1 ---- ---- ---- ---- ---- ---- 
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----

1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----

1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----

1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----
1 ---- ---- ---- ---- ---- ----

But my format turned out like this using the join method:

[[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]][[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]][[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]][[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]]

Upvotes: 2

Views: 579

Answers (2)

Hoog
Hoog

Reputation: 2298

First, I have more formally defined your 3D array:

mylist = [[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]],
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]],
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]],
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]]

This way I have a list of lists of lists, instead of 4 lists of lists that happen to be written in a row, with this format, we can just loop through every element and continue adding to some string as we go:

abcd = ""

for i in mylist:
    for j in i: 
        for k in j:
            if(k): # might as well say if k is not 0
                abcd = abcd +str(k)
            else:
                abcd = abcd +'---'
        abcd = abcd + "\n"
    abcd = abcd + "\n"
print(abcd)

This will give the desired output, but @mypetlion's method is nicer and more pythonic

Upvotes: 0

mypetlion
mypetlion

Reputation: 2524

You need to loop over all of the layers of lists in your nested list. This will require nested list comprehension. Try this:

'\n\n'.join('\n'.join(' '.join(str(x or '----')for x in y)for y in z)for z in myArray)

Note the x or '----' bit. That will evaluate to the first Truthy value. Since zeroes are Falsey, you'll get the dashes if x is zero, or the actual value if it's not.

Upvotes: 2

Related Questions