0

我正在尝试生成具有优先附件的网络。

所以我将从通过边缘连接的两个节点开始:

links = [[1],[0]]

我有示例代码:

weighted = [nodes for v in links for nodes in v]

有人可以翻译 [list] 中的 [something] for v in [list] for [something] in v 的含义吗?我只习惯了列表中 i 的语法:{do stuff}

4

3 回答 3

2
weighted_nodes = []
for v in links:
    for nodes in v:
        weighted_nodes.append(nodes)

本质上就是它正在做的事情

于 2013-11-01T16:48:54.827 回答
0

That's a nested list comprehension. That particular form is used to flatten a nested list.

a = [['a','b'],['c','d','e']]

[elem for sublist in a for elem in sublist]
Out[43]: ['a', 'b', 'c', 'd', 'e']
于 2013-11-01T16:50:17.573 回答
0

From Wikipedia:

A list comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists. It follows the form of the mathematical set-builder notation (set comprehension) as distinct from the use of map and filter functions.

http://en.wikipedia.org/wiki/List_comprehension

于 2013-11-01T16:50:39.100 回答