![](/img/trans.png)
[英]Convert a tupple into integer Python, error message: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple'
[英]Convert a tuple to a string that is that tupple
在代码的某个点上,我有一个需要作为字符串传递的元组列表,但是有一个包含元组的结构元素(即逗号和括号)的字符串。
目前我正在这样做:
listofv = ''
for tu in listof2tuple:
ltu = '(' + tu[0] + ',' + tu[1] + ')'
listofv.append(ltu)
finalstring = ','.join(listofv)
虽然这有效,但似乎很奇怪,因为在IDLE中打印元组显示的字符串恰好是我想要的。
这样做的好方法是什么?
使用代表:
>>> LoT
[(1, 2), (3, 4), (5, 6)]
>>> repr(LoT)
'[(1, 2), (3, 4), (5, 6)]'
您的代码没有为列表添加[..]
大括号。 如果您不希望列表大括号,可以将其删除:
>>> repr(LoT).strip('[]')
'(1, 2), (3, 4), (5, 6)'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.