String representation of arrays in python

Is there anything that does the following in python: Or should I implement it myself?

array = [0, 1, 2]
myString = SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT(array)
print myString

which prints

(0, 1, 2)

thank

+3
source share
4 answers

, str(tuple(array)), @jboxer, . , str(tuple(...)) repr, str - ( print ing , '1, 2', , , int 1 2! -), , . :

>>> array = [0.1, 0.2]
>>> print str(tuple(array))
(0.10000000000000001, 0.20000000000000001)

repr , ( str, , ). , float ( int s, , ;-), :

>>> print '(%s)' % (', '.join(str(x) for x in array))
(0.1, 0.2)

, , !

, list, "", .

+2

, Python . join.

print "(" + ", ".join(array) + ")"

PHP, join implode. ", " - . ,

print "123".join(['a','b','c'])

a123b123c
+5
def SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT (arr):
    return str(tuple(arr))

str unicode, .

+5
SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT = tuple  
+4
source

Source: https://habr.com/ru/post/1725510/


All Articles