如何将嵌套的元组和列表元组转换为Python中的列表列表?

时间:2021-10-14 05:52:19

I have a tuple containing lists and more tuples. I need to convert it to nested lists with the same structure. For example, I want to convert (1,2,[3,(4,5)]) to [1,2,[3,[4,5]]].

我有一个包含列表和更多元组的元组。我需要将它转换为具有相同结构的嵌套列表。例如,我想将(1,2,[3,(4,5)])转换为[1,2,[3,[4,5]]]。

How do I do this (in Python)?

我该怎么做(在Python中)?

4 个解决方案

#1


def listit(t):
    return list(map(listit, t)) if isinstance(t, (list, tuple)) else t

The shortest solution I can imagine.

我能想象的最短的解决方案。

#2


As a python newbie I would try this

作为一个python新手,我会尝试这个

def f(t):
    if type(t) == list or type(t) == tuple:
        return [f(i) for i in t]
    return t

t = (1,2,[3,(4,5)]) 
f(t)
>>> [1, 2, [3, [4, 5]]]

Or, if you like one liners:

或者,如果您喜欢一个衬垫:

def f(t):
    return [f(i) for i in t] if isinstance(t, (list, tuple)) else t

#3


We can (ab)use the fact that json.loads always produces Python lists for JSON lists, while json.dumps turns any Python collection into a JSON list:

我们可以(ab)使用json.loads总是为JSON列表生成Python列表的事实,而json.dumps将任何Python集合转换为JSON列表:

import json

def nested_list(nested_collection):
    return json.loads(json.dumps(nested_collection))

#4


This is what I had come up with, but I like the other's better.

这就是我想出来的,但我更喜欢对方。

def deep_list(x):
      """fully copies trees of tuples or lists to a tree of lists.
         deep_list( (1,2,(3,4)) ) returns [1,2,[3,4]]
         deep_list( (1,2,[3,(4,5)]) ) returns [1,2,[3,[4,5]]]"""
      if not ( type(x) == type( () ) or type(x) == type( [] ) ):
          return x
      return map(deep_list,x)

I see aztek's answer can be shortened to:

我看到aztek的答案可以缩短为:

def deep_list(x):
     return map(deep_list, x) if isinstance(x, (list, tuple)) else x

Update: But now I see from DasIch's comment that this wouldn't work in Python 3.x since map() there returns a generator.

更新:但现在我从DasIch的评论中看到,这在Python 3.x中不起作用,因为map()会返回一个生成器。

#1


def listit(t):
    return list(map(listit, t)) if isinstance(t, (list, tuple)) else t

The shortest solution I can imagine.

我能想象的最短的解决方案。

#2


As a python newbie I would try this

作为一个python新手,我会尝试这个

def f(t):
    if type(t) == list or type(t) == tuple:
        return [f(i) for i in t]
    return t

t = (1,2,[3,(4,5)]) 
f(t)
>>> [1, 2, [3, [4, 5]]]

Or, if you like one liners:

或者,如果您喜欢一个衬垫:

def f(t):
    return [f(i) for i in t] if isinstance(t, (list, tuple)) else t

#3


We can (ab)use the fact that json.loads always produces Python lists for JSON lists, while json.dumps turns any Python collection into a JSON list:

我们可以(ab)使用json.loads总是为JSON列表生成Python列表的事实,而json.dumps将任何Python集合转换为JSON列表:

import json

def nested_list(nested_collection):
    return json.loads(json.dumps(nested_collection))

#4


This is what I had come up with, but I like the other's better.

这就是我想出来的,但我更喜欢对方。

def deep_list(x):
      """fully copies trees of tuples or lists to a tree of lists.
         deep_list( (1,2,(3,4)) ) returns [1,2,[3,4]]
         deep_list( (1,2,[3,(4,5)]) ) returns [1,2,[3,[4,5]]]"""
      if not ( type(x) == type( () ) or type(x) == type( [] ) ):
          return x
      return map(deep_list,x)

I see aztek's answer can be shortened to:

我看到aztek的答案可以缩短为:

def deep_list(x):
     return map(deep_list, x) if isinstance(x, (list, tuple)) else x

Update: But now I see from DasIch's comment that this wouldn't work in Python 3.x since map() there returns a generator.

更新:但现在我从DasIch的评论中看到,这在Python 3.x中不起作用,因为map()会返回一个生成器。