一尘不染

将元组列表转换为列表列表

python

我已经编写了此函数,用于将元组列表转换为列表列表。有没有更优雅的/ Pythonic的方式来做到这一点?

def get_list_of_lists(list_of_tuples):
    list_of_lists = []                                                          
    for tuple in list_of_tuples:
        list_of_lists.append(list(tuple))

    return list_of_lists

阅读 185

收藏
2020-12-20

共1个答案

一尘不染

您可以使用列表推导:

>>> list_of_tuples = [(1, 2), (4, 5)]
>>> list_of_lists = [list(elem) for elem in list_of_tuples]

>>> list_of_lists
[[1, 2], [4, 5]]
2020-12-20