我正在尝试生成具有优先附件的网络。
所以我将从通过边缘连接的两个节点开始:
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}
我正在尝试生成具有优先附件的网络。
所以我将从通过边缘连接的两个节点开始:
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}
weighted_nodes = []
for v in links:
for nodes in v:
weighted_nodes.append(nodes)
本质上就是它正在做的事情
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']
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.