Here is my string
a = '(20.0, 8.0, 8.0, 37.0)'
and i want only first three items in this format
str = 20.0x8.0x8.0
and store the last item in a list.
leftover = [37.0]
Earlier I had only three dimensions in the string so I was using replace function multiple times to convert it.
converted = str(''.join(str(v) for v in a)).replace("(","").replace(")","").replace(", ","x")
I know it's not the most pythonic way but it was doing the trick. But I have no clue how to remove an element from str(tuple)
Use ast.literal_eval
:
import ast
a = '(20.0, 8.0, 8.0, 37.0)'
a_tupl = ast.literal_eval(a)
string = 'x'.join(map(str, a_tupl[:3])) # 20.0x8.0x8.0
leftover = [a_tupl[-1]] # [37.0]
str
, because it shadows the built-in.