将元组列表转换为列表列表 [英] Convert a list of tuples to a list of lists

查看:80
本文介绍了将元组列表转换为列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这个函数来将元组列表转换为列表列表.有没有更优雅/Pythonic 的方式来做到这一点?

def get_list_of_lists(list_of_tuples):list_of_lists = []对于 list_of_tuples 中的元组:list_of_lists.append(list(tuple))返回 list_of_lists

解决方案

您可以使用列表理解:

<预><代码>>>>list_of_tuples = [(1, 2), (4, 5)]>>>list_of_lists = [list_of_tuples 中的 elem 的 list(elem)]>>>list_of_lists[[1, 2], [4, 5]]

I've written this function to convert a list of tuples to a list of lists. Is there a more elegant / Pythonic way of doing this?

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

解决方案

You can use list comprehension:

>>> 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]]

这篇关于将元组列表转换为列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆