如何转换列表
[1, 2, 3, 4, 5]
到元组列表
[(1, 2, 3, 4, 5)]
和 转换元组
(1, 2, 3, 4, 5)
到元组列表
[(1, 2, 3, 4, 5)]
答案 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)]