![](/img/trans.png)
[英]python - Is it possible to convert a string and put it into a list [] containing tuple ()?
[英]Convert list containing tuple to string
我有一個包含元組的列表。 我需要將整個列表轉換為字符串以進行壓縮。 這段代碼在Python 2.7中運行良好:
tt = '{}'.format(tt)
但是在Python 2.6中出現以下錯誤:
hist = '{}'.format(hist)
ValueError: zero length field name in format
tt
的數據看起來像[(2, 3, 4), (34, 5, 7)...]
除了升級Python版本以外,還有其他解決方法嗎?
在替換字段中輸入一個索引:
tt = '{0}'.format(tt)
或者只是使用:
tt = str(tt)
在2.6中引入str.format
之前,它還將支持Python版本。
演示:
>>> tt = [(2, 3, 4), (34, 5, 7)]
>>> "{0}".format(tt)
'[(2, 3, 4), (34, 5, 7)]'
>>> str(tt)
'[(2, 3, 4), (34, 5, 7)]'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.