3

Is there anything that performs the following, in python? Or will I have to implement it myself?

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

which prints

(0, 1, 2)

Thanks

3
  • 1
    'print array' works... are you specifically interested in the parentheses? Commented Dec 13, 2009 at 4:30
  • You should know and use the various names of the sequence types, which are [list], (tuple,..) and {'dict':..}. They all have different behaviour. What you want is to convert a list to a tuple, and that can be done just by print tuple(list). See also Prasoon's answer, which is correct. Commented Dec 13, 2009 at 6:28
  • 1
    I think everyone has taken the question too literally, and what devoured elysium is looking for is just str(array). Commented Dec 13, 2009 at 7:01

4 Answers 4

5

You're in luck, Python has a function for this purpose exactly. It's called join.

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

If you're familiar with PHP, join is similar to implode. The ", " above is the element separator, and can be replaced with any string. For example,

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

will print

a123b123c
Sign up to request clarification or add additional context in comments.

1 Comment

This will work in your example, but not in the one the submitter gave, since he used ints. You could use ", ".join([str(x) for x in array]) though.
5
def SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT (arr):
    return str(tuple(arr))

Replace str with unicode if you need to.

Comments

4
SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT = tuple  

2 Comments

The OP claims he wants a string (though his use of print obfuscates that;-).
I know. My comment was more of a tongue-in-cheek remark, particularly because I got a kick out of his use of "SOME_FUNCTION_THAT_TAKES_AN_ARRAY_AS_INPUT". :-)
2

If your array's items are specifically integers, str(tuple(array)) as suggested in @jboxer's answer will work. For most other types of items, it may be more of a problem, since str(tuple(...)) uses repr, not str -- that's really needed as a default behavior (otherwise printing a tuple with an item such as the string '1, 2' would be extremely confusing, looking just like a string with the two int items 1 and 2!-), but it may or may not be what you want. For example:

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

With floating point numbers, repr emits many more digits than make sense in most cases (while str, if called directly on the numbers, behaves a bit better). So if your items are liable to be floats (as well as ints, which would need no precaution but won't be hurt by this one;-), you might better off with:

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

However, this might produce ambiguous output if some of the items are strings, as I mentioned earlier!

If you know what types of data you're liable to have in the list which you call "array", it would give a better basis on which to recommend a solution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.