在python 3.3中,如何将嵌套列表转换为元组列表?

在python 3.3中,如何将嵌套列表转换为元组列表?,python,list,tuples,nested-lists,type-conversion,Python,List,Tuples,Nested Lists,Type Conversion,在Python3.3中,我试图将嵌套列表转换为元组列表。然而,我似乎没有这样做的逻辑 输入如下所示: >>> nested_lst = [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']] 所需的输出应如下所示: nested_lst_of_tuples = [('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')] 只需使用列表: nested_lst_of_tupl

在Python3.3中,我试图将嵌套列表转换为元组列表。然而,我似乎没有这样做的逻辑

输入如下所示:

>>> nested_lst = [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']]
所需的输出应如下所示:

nested_lst_of_tuples = [('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')]

只需使用列表:

nested_lst_of_tuples = [tuple(l) for l in nested_lst]
演示:

您可以使用:

这相当于列表理解,只是
map
返回一个生成器而不是列表

>>> nested_lst = [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']]
>>> [tuple(l) for l in nested_lst]
[('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')]
[tuple(l) for l in nested_lst]
>>> list(map(tuple, [['tom', 'cat'], ['jerry', 'mouse'], ['spark', 'dog']]))
[('tom', 'cat'), ('jerry', 'mouse'), ('spark', 'dog')]