[英]Converting a list of strings of numbers to tuples with integers Python3
[英]Converting fetchall list of tuples into strings? Python3
我在sqlite3上使用SELECT命令,然后通過fetchall讀取元組列表...而我得到:
[(1, 'Pencil', '99', 25.0, 30.0, 5.0), (2, 'Marker', '40', 30.0, 20.0, -10.0)]
我如何將其轉換為:
[('1', 'Pencil', '99', '25.0', '30.0', '5.0'), (2, 'Marker', '40', '30.0', '20.0', '-10.0')]
如果列表中還有更多的元組怎么辦?
您可以簡單地使用map
來實現:
a = [(1, 'Pencil', '99', 25.0, 30.0, 5.0), (2, 'Marker', '40', 30.0, 20.0, -10.0)]
a_new = [tuple(map(str, i)) for i in a]
>>> print(a_new)
[('1', 'Pencil', '99', '25.0', '30.0', '5.0'), ('2', 'Marker', '40', '30.0', '20.0', '-10.0')]
您可以在這里快速嘗試: DEMO
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.