带有字符串列表的拆开/平坦的不平衡列表
鉴于这种情况,我有以下列表:
['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]]
我希望将其转换为类似的内容:
['graph_edges', 'graph_nodes', 'graph_nodes', 'graph_edges2', 'graph_nodes2', 'graph_nodes2']
# I would list(set(thislist)) afterwards
已经有很多解决方案,但奇怪的是,对于我的情况,我无法完成任何有意义的事情:
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
相同:
sum(['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]], [])
这单行展开太多:
> [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']]
或者与itertools:
>!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
中尝试过这些,所以总是有可能出现错误
我当前(不起作用)且非常不满意的解决方案是这里:
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
达到< code>['graph_edges2', ['graph_nodes2'], ['graph_nodes2']] 它失败了,但我无法想到有意义的东西。我可以制作一个生成新值而不是返回值的生成器,或者迭代每个元素并使其成为一个可以溶解的列表。但这些想法都不能让我信服
Given the case I have the following List:
['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]]
And I wish to convert it to something like:
['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 but strangely for my case I can't get anything meaningful done:
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
Same with sum
:
sum(['graph_edges', ['graph_nodes'], ['graph_nodes'], ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]], [])
This one-liner unwraps too much:
> [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']]
Or with itertools:
>!list(itertools.chain(*lol))
['g', 'r', 'a', 'p', 'h', '_', 'e', 'd', 'g', 'e', 's', 'graph_nodes', 'graph_nodes', 'graph_edges2', ['graph_nodes2'], ['graph_nodes2']]
Disclaimer: I tried these in ipdb
, so there's always a chance of a bug
My current (not working) and very unsatisfying solution is this here:
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
When element
reaches ['graph_edges2', ['graph_nodes2'], ['graph_nodes2']]
it's failing, but I won't be able to think of something meaningful. I could either make a generator that yields new values instead of returns or iterate through every element and make it a list which can be dissolved. But none of these ideas are convincing to me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用递归来考虑列表可以任意深度嵌套的事实:
此输出:
You need to use recursion to account for the fact that the lists can be nested arbitrarily deep:
This outputs: