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
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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, sincestr(tuple(...))usesrepr, notstr— that’s really needed as a default behavior (otherwiseprinting a tuple with an item such as the string'1, 2'would be extremely confusing, looking just like a string with the two int items1and2!-), but it may or may not be what you want. For example:With floating point numbers,
repremits many more digits than make sense in most cases (whilestr, if called directly on the numbers, behaves a bit better). So if your items are liable to befloats (as well asints, which would need no precaution but won’t be hurt by this one;-), you might better off with: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
listwhich you call “array”, it would give a better basis on which to recommend a solution.