简体   繁体   English

如何将元组转换为元组列表?

[英]How to convert a tuple to a list of tuple?

For example I have a tuple (1,2,3) , how to convert it into [(1,2,3)] ?例如我有一个元组(1,2,3) ,如何将其转换为[(1,2,3)] I tried list method but it seems doesn't work.我尝试了list方法,但似乎不起作用。

l = (1,2,3) -> l =[(1,2,3)]

只需将元组放入列表中即可。

l = [(1, 2, 3)]
l_tuple = (1, 2, 3)
l_list = [l_tuple]
l_list_items = list(l_tuple)

>>> print l_list
[(1, 2, 3)]

>>> print l_list_items
[1, 2, 3]

Not that there is a difference between using the square braces and the list() function to construct your list.并不是说使用方括号和list()函数来构建列表之间存在差异。

>>> l = (1,2,3)
>>> print list([l])

Create an empty list and append tuple to it.创建一个空列表并将元组附加到它。

>>>t = (1,2,3)
>>>a = []
>>>a.append(t)
>>>a
[(1,2,3)]
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM