首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用String展开/平放不平衡列表

用String展开/平放不平衡列表
EN

Stack Overflow用户
提问于 2022-04-06 16:49:17
回答 2查看 53关注 0票数 1

鉴于这种情况,我有以下清单:

代码语言:javascript
运行
复制
['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]]

我想把它转换成:

代码语言:javascript
运行
复制
['graph_edges', 'graph_nodes', 'graph_nodes', 'graph_edges2', 'graph_nodes2', 'graph_nodes2'] 
# I would list(set(thislist)) afterwards

There is a ton of solutions out there already,但奇怪的是,就我的情况而言,我无法做任何有意义的事情:

代码语言:javascript
运行
复制
from functools import reduce
import operator
reduce(operator.concat,['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]])
*** TypeError: can only concatenate str (not "list") to str

同样适用于sum

代码语言:javascript
运行
复制
sum(['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]], [])

这艘客轮开得太多了:

代码语言:javascript
运行
复制
> [item for sublist in ['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]] for item in sublist]
['g', 'r', 'a', 'p', 'h', '_', 'e', 'd', 'g', 'e', 's', 'graph_nodes', 'graph_nodes', 'graph_edges2', ['graph_nodes2'], ['graph_nodes2']]

或者使用迭代工具:

代码语言:javascript
运行
复制
>!list(itertools.chain(*lol))
['g', 'r', 'a', 'p', 'h', '_', 'e', 'd', 'g', 'e', 's', 'graph_nodes', 'graph_nodes', 'graph_edges2', ['graph_nodes2'], ['graph_nodes2']]

免责声明:我在ipdb中尝试过这些,所以总是有可能出现错误

我目前(不工作)和非常不满意的解决办法如下:

代码语言:javascript
运行
复制
retlist= []
dedefined=['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]]

for element in dedefined:
    if isinstance(element,list):
            retlist+=self.getSingleElement(element)
        else:
            retlist.append(element)
    return list(set(retlist))

@classmethod
def getSingleElement(cls,element):
    if isinstance(element,list):            
            return cls.getSingleElement(*element)
    else: return element

element到达['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]时,它是失败的,但我无法想到有意义的东西。我可以创建一个生成器来生成新的值,而不是返回,或者迭代每个元素,并使其成为一个可以被分解的列表。但这些想法对我来说都没有说服力

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-06 17:00:42

您需要使用递归来解释列表可以任意嵌套的深层次这一事实:

代码语言:javascript
运行
复制
def flatten(lst):
    result = []
    for item in lst:
        # You can use:
        # if not isinstance(item, list):
        # if you have other items besides integers in your nested list.
        if isinstance(item, str):
            result.append(item)
        else:
            result.extend(flatten(item))
    return result

这一产出如下:

代码语言:javascript
运行
复制
['graph_edges', 'graph_nodes', 'graph_nodes',
 'graph_edges2', 'graph_nodes2', 'graph_nodes2']
票数 1
EN

Stack Overflow用户

发布于 2022-04-06 17:02:15

代码语言:javascript
运行
复制
def flatten(array):
    flat = []
    for member in array:
        if isinstance(member, (tuple, list)):
            flat.extend(flatten(member))
        else:
            flat.append(member)
    return flat
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71770712

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档