I have a problem with the behavior of list comprehension for 2D arrays created with numpy: The behavior is dependent of the length of the list:
import numpy as np
oneElement = np.ones([2,3]) # one super tiny images in a list
twoElement = [oneElement, oneElement] # two super tiny images in a list
print([E.shape for E in twoElement]) # here E represents an img
>>>> [(2, 3), (2, 3)]
print([E.shape for E in oneElement]) # here E represents only a row (of an img)
>>>> [(3,), (3,)]
As you see, the first print() printed the dimensions of an image. That is, because the E is representing an 2D array, or an full image respectively. This is the behavior I want!
The second list comprehension however, only returns single rows. I assume because "rows" is the only thing to iterate over, here.
Ultimately, I (always) want the first behavior, iterating over full images, not single rows, independent of the number of images in the list. Is there a way to "tell" the list comprehension to do so? Or do I always have to include a if (type(twoElement) is list): beforehand?
That will make my source code quite ugly, because I have many such comprehensions, but I can not guarantee that there is always more than one image in the list...
Thanks for advice!
Aucun commentaire:
Enregistrer un commentaire