Python将列表转换为元组和列表的列表元组列表的元组

时间:2018-04-26 21:00:59

标签: python

如何转换列表

    [1, 2, 3, 4, 5]

元组列表

    [(1, 2, 3, 4, 5)]

和 转换元组

    (1, 2, 3, 4, 5)

元组列表

    [(1, 2, 3, 4, 5)]

4 个答案:

答案 0 :(得分:0)

从列表中:

[tuple(x)]

来自元组:

[x]

>>> x = [1,2,3]
>>> [tuple(x)]
[(1, 2, 3)]
>>> x = (1, 2, 3)
>>> [x]
[(1, 2, 3)]

答案 1 :(得分:0)

arr = [1, 2, 3, 4]
print(arr)
tpl = (arr,)
print(type(tpl), tpl)

输出:

[1, 2, 3, 4]
<class 'tuple'> ([1, 2, 3, 4],)

案例2:

tpl_2 = (1, 2, 3, 4)
print(tpl_2)
arr_2 = [tpl_2]
print(type(arr_2), arr_2)

输出:

(1, 2, 3, 4)
<class 'list'> [(1, 2, 3, 4)]

答案 2 :(得分:0)

如何做到以下几点: -

your_list = [1,2,3,4]
new_list = [tuple(your_list)]

在第二种情况下: -

your_tuple = (1,2,3,4)
new_list = [your_tuple]

答案 3 :(得分:0)

试试吧!

l=[1, 2, 3, 4, 5]
t=tuple(i for i in l)
t

输出:

(1, 2, 3, 4, 5)

tl = [t]
tl

输出:

[(1, 2, 3, 4, 5)]