我编写了这个函数来将元组列表转换为列表列表。有没有更优雅/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]]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)