我已经编写了此函数,用于将元组列表转换为列表列表。有没有更优雅的/ 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
您可以使用列表推导:
>>> 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]]