[英]networkx and iteration of lists - piece of code I cannot understand
I'm new to the python language and have been working with the networkx
package. 我是python语言的新手,并且一直在使用
networkx
软件包。 Basically I have a list of customers and producers and want a function that retrieves the list of the current list based on these types. 基本上,我有一个客户和生产者的列表,并且想要一个基于这些类型来检索当前列表的列表的函数。
Here is the relevant code for the function that retrieves customers: 以下是用于检索客户的功能的相关代码:
def customers_iter(self, data=False):
""" Return an iterator over all customers.
If the network is changed during iteration, the iterator becomes
invalid.
Parameters
-----------
data - if True, return a list of (name, attributes) pairs, such
that attributes == net.node[name]. Otherwise,
only a list of customer names is returned. Default is
False.
"""
if data:
return (n for n in self.nodes_iter(data=True)
if self.node[n[0]]["type"] == "customer")
else:
return (n for n in self.nodes_iter()
if self.node[n]["type"] == "customer")
My question is specifically regarding the if- and else statement. 我的问题特别是关于if-和else语句。 What is the point if first checking the first node n[0]?
如果首先检查第一个节点n [0],那有什么意义? Doesn't the statement in the else-section define exactly the same thing?
else节中的语句是否定义完全相同的东西?
Regards, jazy 问候,贾兹
According to the doc comment: 根据文档评论:
data - if True, return a list of (name, attributes) pairs, such
that attributes == net.node[name]. Otherwise,
only a list of customer names is returned. Default is
False.
Assuming, that data
parameter has the same meaning in the self.node_iter
function, the first branch ( if
) should filter pairs of (name, attributes)
, however the second one ( else
) should filter only name
s. 假设
data
参数在self.node_iter
函数中具有相同的含义,则第一个分支( if
)应该过滤(name, attributes)
,而第二个分支( else
)应该仅过滤name
s。
Assuming, also, that self.node
is a dictionary-like structure for holding associative pairs name
-> node
, it is easy to see, that in the first branch we have to retrieve the name
from the tuple
(which is n[0]
), while in the second branch we could just use n
as the node name. 同样,假设
self.node
是一个类似字典的结构,用于保存关联对name
-> node
,很容易看出,在第一个分支中,我们必须从tuple
检索name
(即n[0]
),而在第二个分支中,我们可以仅使用n
作为节点名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.