How to Get Rid of Array Brackets While Printing

How to remove square brackets from list in Python?

You could convert it to a string instead of printing the list directly:

print(", ".join(LIST))

If the elements in the list aren't strings, you can convert them to string using either repr (if you want quotes around strings) or str (if you don't), like so:

LIST = [1, "foo", 3.5, { "hello": "bye" }]
print( ", ".join( repr(e) for e in LIST ) )

Which gives the output:

1, 'foo', 3.5, {'hello': 'bye'}

How to get rid of array brackets while printing

You could do:

extension Array {
var minimalDescription: String {
return " ".join(map { "\($0)" })
}
}

["1", "2", "3", "4"].minimalDescription // "1 2 3 4"

With Swift 2, using Xcode b6, comes the joinWithSeparator method on SequenceType:

extension SequenceType where Generator.Element == String {
...
public func joinWithSeparator(separator: String) -> String
}

Meaning you could do:

extension SequenceType {
var minimalDescrption: String {
return map { String($0) }.joinWithSeparator(" ")
}
}

[1, 2, 3, 4].minimalDescrption // "1 2 3 4"

Swift 3:

extension Sequence {
var minimalDescription: String {
return map { "\($0)" }.joined(separator: " ")
}
}

Print array without brackets and commas

Basically, don't use ArrayList.toString() - build the string up for yourself. For example:

StringBuilder builder = new StringBuilder();
for (String value : publicArray) {
builder.append(value);
}
String text = builder.toString();

(Personally I wouldn't call the variable publicArray when it's not actually an array, by the way.)

Python - How to delete/hide brackets and array(value) when printing an array?

You have created (n,1) shaped arrays

In [283]: x = np.array([[1],[2],[3]]); y = np.array([['one'],['two'],['three']])

In [284]: x
Out[284]:
array([[1],
[2],
[3]])

In [285]: y
Out[285]:
array([['one'],
['two'],
['three']], dtype='<U5')

In [286]: x.shape
Out[286]: (3, 1)

If you iterate on the first dimension, the elements are (1,) shaped arrays, and display as such:

In [287]: for i in range(3):
...: print(y[1],x[i])
...:
['two'] [1]
['two'] [2]
['two'] [3]

If the arrays are 1d, flattened (if needed):

In [288]: x1 = x.ravel(); y1 = y.ravel()

In [289]: x1.shape
Out[289]: (3,)
In [291]: x1,y1
Out[291]: (array([1, 2, 3]), array(['one', 'two', 'three'], dtype='<U5'))

Now y1[0] is a string, and x1[0] is a number:

In [292]: for i in range(3):
...: print(y1[1],x1[i])
...:
two 1
two 2
two 3

And adding a bit of formatting to the print:

In [294]: for i in range(3):
...: print(f'{y1[1]} = {x1[i]}')
...:
two = 1
two = 2
two = 3

You could also use 2d indexing on the 2d arrays:

In [297]: for i in range(3):
...: print(f'{y[1,0]} = {x[i,0]}')
...:
two = 1
two = 2
two = 3

Print list without brackets in a single row

print(', '.join(names))

This, like it sounds, just takes all the elements of the list and joins them with ', '.

How to remove the square brackets from a list when it is printed/output

You can use map() to convert the numbers to strings, and use " ".join() or ", ".join() to place them in the same string:

mylist = [4, 5]
print(" ".join(map(str, mylist)))
#4 5
print(", ".join(map(str, mylist)))
#4, 5

You could also just take advantage of the print() function:

mylist = [4, 5]
print(*mylist)
#4 5
print(*mylist, sep=", ")
#4, 5

Note: In Python2, that won't work because print is a statement, not a function. You could put from __future__ import print_function at the beginning of the file, or you could use import __builtin__; getattr(__builtin__, 'print')(*mylist)

How to print a Numpy array without brackets?

You can use the join method from string:

>>> a = [1,2,3,4,5]
>>> ' '.join(map(str, a))
"1 2 3 4 5"

Python how to remove brackets when printing a list of tuples?

You're kind of misunderstanding. In general, you can never remove characters from a tuple. Since they are immutable, you can only create a new one and build it using the desired elements of the original. The reason there are square brackets is because the output of your "names and scores" was created using list comprehension. That is to say, it's a list. The square brackets are there to tell you it's a list, so you can't "remove them". If you want to see each tuple without the square brackets you can iterate through the list of tuples and print each tuple. That way you'd get something like:

("Fred's", 13)
("Jack's", 12)

If you want to remove the brackets round and square, you can refer to the tuple with indexes for the values. You can do:

print("PLAYER:\t\tSCORE:")
for i in names_and_scores[:5]:
print("{}\t\t{}".format(i[0],i[1]))

This is going to iterate through the desired portion of your list (up till the fifth, it seems) and then print something like...

PLAYER:    SCORE:
Jack's 12
Fred's 13

You can ignore the 's with...

print("PLAYER:\t\tSCORE:")
for i in names_and_scores[:5]:
print("{}\t\t{}".format(i[0][:-2],i[1]))

Here's my code and output:

list = [("fred's", 13), ("jack's", 19), ("mark's", 16), ("amy's", 12), ("finlay's", 17)]
print("PLAYER:\t\tSCORE")
for i in list:
print("{}\t\t{}".format(i[0][:-2],i[1]))

PLAYER: SCORE
fred 13
jack 19
mark 16
amy 12
finlay 17


Related Topics



Leave a reply



Submit