将列表的多级列表展平到单个级别 [英] Flattening a multilevel list of lists to a single level

查看:74
本文介绍了将列表的多级列表展平到单个级别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要展平多级列表的所有级别:

I need to flatten all the levels of a multi-level list:

import itertools

pool = [[[[[0,2],[1,3]],[[3,2],[1,0]]],"PdPd","PrisonerDilemma"], 
[[[[0,3],[1,2]],[[2,3],[1,0]]],"ShSh","StagHunt"],
[[[[1,2],[3,0]],[[3,2],[1,0]]],"ChCh","Chicken"],
[[[[2,1],[0,3]],[[3,1],[0,2]]],"BaBa","Battle"]]

def flat3 (pool):
    for game in pool:
        l = list(itertools.chain.from_iterable(game[0]))
        print(l)

结果:

flat3 (pool)
[[0, 2], [1, 3], [3, 2], [1, 0]]
[[0, 3], [1, 2], [2, 3], [1, 0]]
[[1, 2], [3, 0], [3, 2], [1, 0]]
[[2, 1], [0, 3], [3, 1], [0, 2]]

因此,目标是隔离并返回每个项目中的第一级,仅包含数字,例如:

So, the objective is to isolate and return the first level in each item, containing only the numbers, such as:

[[[[[0,2],[1,3]],[[3,2],[1,0]]],"PdPd","PrisonerDilemma"]

然后,我需要将所有内容都展平到相同的水平,例如:

Then I need everything to be flattened to the same level, like:

[0,2,1,3,3,2,1,0]

我知道关于此主题的材料很多,我已经找到并尝试了许多方法来完成此操作,但是似乎没有一个方法可以在多个级别上使用,我想知道是否有一种有效的方法可以做到这一点,而无需多次重复同一命令.有人可以帮我吗?

I know there is a lot of material on this topic and I have found and tried many ways of doing this, however none seems to work with more than one level, I want to know if there is an efficient way to do this, without repeating the same command many times. Could anyone help me?

谢谢

推荐答案

通常,第一步是查看您的数据结构.

The first step, as usual, is to look at your data structure.

el = [
         [
             [[0,2],[1,3]],
             [[3,2],[1,0]]
         ],
         "PdPd",
         "PrisonerDilemma"
     ]

这是结构中的每个单独元素.暂时我们将忽略这个最外层的结构,几乎可以肯定它不应该是一个列表(在我看来,它更像一个元组),而只是关注我们所拥有的.

This is each individual element in your structure. We'll ignore for the time being that this outermost structure almost certainly shouldn't be a list (it looks more like a tuple to me) and just focus on what we've got.

el[0] = [ [[0, 2], [1, 3]],
          [[3, 2], [1, 0]] ]
el[1] = # a string
el[2] = # another string, we don't care about these

现在,我们将其简化为数字列表列表.我们可以对其进行操作.

Now we've reduced it to a list of lists of LISTS of numbers. This one we can operate on.

def flatten(lst):
    for el in lst:
        if isinstance(el, list):  # N.B. this only works for lists, not general
                                  # collections e.g. sets, tuples, dicts, etc...
            # recurse
            yield from flatten(el)
        else:
            # generate
            yield el

您的结果是将该功能应用于最外层列表中每个项目的第一个元素.

your result then being to apply this function to the first element of each item in the outermost list.

result = [flatten(sublst[0]) for sublst in big_list]

请注意,这将创建一个生成器对象列表,需要使用这些对象才能实际生成值.如果您出于某些原因实际上需要列表本身,请强制转换为列表:

Note that this creates a list of generator objects, which need to be consumed to actually produce values. If you actually need the lists themselves for some reason, cast to list explicitly:

result = [list(flatten(sublst[0])) for sublst in big_list]

这篇关于将列表的多级列表展平到单个级别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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