Python Forum
Printing array without commas
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing array without commas
#1
Hello,
I have got this output from my script:
Output:
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
I want to cover it to:
Output:
[[1.0 2.0 3.0] [4.0 5.0 6.0] [7.0 8.0 9.0]]
I tried:
a = np.asarray(a)
but output is:
Output:
array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
How resolve this?

I found that
a = np.asarray
worked correctly, but there was a problem with command print. When I used:
print ("a", a)
it gave me
Output:
('a', array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
But when I used:
print a
the output was:
Output:
[[1.0 2.0 3.0] [4.0 5.0 6.0] [7.0 8.0 9.0]]
Reply
#2
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:
Output:
[[ 1.0 2.0 3.0 ] [ 4.0 5.0 6.0 ] [ 7.0 8.0 9.0 ]]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Commas issue in variable ddahlman 6 382 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  help with commas in print functions kronhamilton 11 3,418 Feb-10-2022, 02:02 PM
Last Post: mishraakash
  Help|CSV Writing on List with Inner Commas soothsayerpg 2 2,347 Jul-20-2019, 06:59 AM
Last Post: scidam
  [split] Automate the boring stuff, inserting commas in list srikanth 1 2,106 Jul-02-2019, 02:29 PM
Last Post: metulburr
  Automate the boring stuff, inserting commas in list DJ_Qu 3 4,687 Apr-21-2019, 03:52 PM
Last Post: perfringo
  accessing array without commas rjnabil1994 1 2,491 Feb-10-2019, 06:29 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020