Search code examples
pythonnumpyellipsis

Ellipses when converting list of numpy arrays to string in python 3


I have a list of numpy arrays. I want to convert the list of arrays to a string. So that there will be a long string of arrays like '[ stuff ], [ stuff2 ]', etc. Each array has 192 elements. Conversion works when I do str(myList) if the list has 5 arrays or less. If it has 6 arrays, I get back truncated arrays with ellipses. Why is this? How can I stop it?.

I have examined the arrays themselves and they do not in fact contain ellipses, they contain the correct values.

I further looked into it and if I do something like str(myList[0:5]) it works on the first 5 arrays, but that 6th array always goes to ellipses. Note that this is not just ellipses when printing to screen either, I'm saving this variable and when I look at the saved text it has the ellipses.


Solution

  • From a quick look, the only way is to use numpy.set_printoptions:

    import numpy as np
    
    a = np.random.randint(5, size=(6, 192))
    s1 = str(a)
    np.set_printoptions(threshold = np.prod(a.shape))
    s2 = str(a)
    
    print('...' in s1)
    print('...' in s2)
    

    gives

    True
    False
    

    on my Ubuntu 14.04 system, Python 2.7, Numpy 1.8.2

    I would restore the default to 1000 after changing it, and, in my opinion, the function numpy.array2string should have a threshold argument.