从列表中的元组中删除字符
我有一个这种形式的元组列表(由数据库查询生成):
[(280.73,), (281.359,), (280.630,)]
我想删除 () 和逗号来实现类似的功能,使其更兼容形成 JSON。
[280.73,281.359,280.630]
做到这一点最简单的方法是什么?
I have a list of tuples in this form (generated by a DB query):
[(280.73,), (281.359,), (280.630,)]
I would like to remove the () and commas to achieve something like this, making it more compatible to form into a JSON.
[280.73,281.359,280.630]
What is the easiest way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
给定一个单元素
元组
的列表
(我们称之为l
(对于list))。您想将其展平为元素列表。
列表理解,提取每个元组的第一个元素将完成这项工作:
Given a
list
of single-elementtuple
s (let's call itl
(forlist
)). You want to flatten this into a list of elements.A list comprehension, extracting the first element of each tuple will do the job:
最简单的可能是通过列表理解:
The easiest is probably through list comprehension:
这些示例将适用于任意元素数量的元组。
These examples will work with arbitrary elements number of tuples.
作为一种替代方法,您也可以这样做:
要将其转换为列表,请将其传递到
list
函数中:Just as an alternative hack, you can do this as well:
To convert it to a list, pass it into the
list
function: