Python怎么拉平嵌套列表

2024-12-04 17:04:30
推荐回答(2个)
回答1:

方法有很多,目前了解到的各方面都比较好,也很pythonic的方法是:
  def flatten(l): for el in l: if hasattr(el, "__iter__") and not isinstance(el, basestring): for sub in flatten(el): yield sub else: yield el
  l = [1, 2, [3, 4, [5, 6]], ["abc", "def"]] l2 = [x for x in flatten(l)] print l2 #[1, 2, 3, 4, 5, 6, "abc", "def"]

回答2:

private:
friend class Graph;//允许Graph类任意访问
NameType m_vertexName;//顶点中的数据内容
Edge *padjEdge;//顶点的邻边