![]() |
Printing array without commas - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Printing array without commas (/thread-9431.html) |
Printing array without commas - pawlaczkaj - Apr-08-2018 Hello, I have got this output from my script: I want to cover it to: I tried:a = np.asarray(a)but output is: How resolve this?I found that a = np.asarrayworked correctly, but there was a problem with command print. When I used: print ("a", a)it gave me But when I used:print athe output was:
RE: Printing array without commas - Larz60+ - Apr-08-2018 If you try to print the array as an array, it will always contain comma's because that is how python represents the array contents. You need to create a function that loops through the array and prints without commas. Something like: class PrintList: def print_list(self, aname): firstitem = True if isinstance(aname, list): print('[', end='') for item in aname: if isinstance(item, list): if firstitem: firstitem = False else: print() self.print_list(item) else: print(' {} '.format(item), end='') print(']', end = '') else: print('Not a list') def tryit(): p = PrintList() p.print_list([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]) if __name__ == '__main__': tryit()test:
|