class VertexSeq(_VertexSeq): (source)
表示图中顶点序列的类。
这个类最容易通过vs字段访问,该字段返回图中所有顶点的有序序列。可以通过调用VertexSeq.select()方法来细化顶点序列。VertexSeq.select()也可以通过简单地调用VertexSeq对象来访问。
创建引用给定图的顶点序列的另一种方法是直接使用构造函数:
>>> g = Graph.Full(3) >>> vs = VertexSeq(g) >>> restricted_vs = VertexSeq(g, [0, 1])
可以通过索引顶点序列对象来访问各个顶点。它也可以用作可迭代对象,甚至可以在列表推导式中使用:
>>> g=Graph.Full(3) >>> for v in g.vs: ... v["value"] = v.index ** 2 ... >>> [v["value"] ** 0.5 for v in g.vs] [0.0, 1.0, 2.0]
顶点集也可以用作字典,其中键是属性名称。与键对应的值是由序列选择的每个顶点的给定属性的值。
>>> g=Graph.Full(3) >>> for idx, v in enumerate(g.vs): ... v["weight"] = idx*(idx+1) ... >>> g.vs["weight"] [0, 2, 6] >>> g.vs.select(1,2)["weight"] = [10, 20] >>> g.vs["weight"] [0, 10, 20]
如果您指定的序列比VertexSeq中的顶点数量短,则该序列将被重复使用:
>>> g = Graph.Tree(7, 2) >>> g.vs["color"] = ["red", "green"] >>> g.vs["color"] ['red', 'green', 'red', 'green', 'red', 'green', 'red']
你甚至可以传递一个单独的字符串或整数,它将被视为长度为1的序列:
>>> g.vs["color"] = "red" >>> g.vs["color"] ['red', 'red', 'red', 'red', 'red', 'red', 'red']
顶点序列的一些方法只是Graph对象中相应方法的代理方法。一个这样的例子是VertexSeq.degree():
>>> g=Graph.Tree(7, 2) >>> g.vs.degree() [2, 3, 3, 1, 1, 1, 1] >>> g.vs.degree() == g.degree() True
| 方法 | __call__ |
select() 的简写表示法 |
| 方法 | attributes |
返回与此顶点序列关联的图中所有顶点属性的列表。 |
| 方法 | find |
返回符合某些条件的顶点序列中的第一个顶点。 |
| 方法 | select |
根据某些条件选择顶点序列的子集 |
继承自 VertexSeq:
| 方法 | attribute |
返回图的顶点属性名称列表 |
| 方法 | get |
返回列表中所有顶点的给定顶点属性的值。 |
| 方法 | set |
为所有顶点设置给定顶点属性的值 |
| 方法 | _reindex |
重新创建将顶点名称映射到ID的字典。 |
igraph._igraph.VertexSeq.find返回符合某些条件的顶点序列的第一个顶点。
选择标准与VertexSeq.select允许的标准相同。有关更多详细信息,请参阅VertexSeq.select。
例如,要在图 g 中找到名称为 foo 的第一个顶点:
>>> g.vs.find(name="foo") #doctest:+SKIP
查找任意孤立的顶点:
>>> g.vs.find(_degree=0) #doctest:+SKIP
igraph._igraph.VertexSeq.select根据某些条件选择顶点序列的子集
选择标准可以通过位置参数和关键字参数来指定。位置参数总是在关键字参数之前处理。
- If the first positional argument is None, an empty sequence is returned.
- If the first positional argument is a callable object, the object will be called for every vertex in the sequence. If it returns True, the vertex will be included, otherwise it will be excluded.
- If the first positional argument is an iterable, it must return integers and they will be considered as indices of the current vertex set (NOT the whole vertex set of the graph -- the difference matters when one filters a vertex set that has already been filtered by a previous invocation of
VertexSeq.select(). In this case, the indices do not refer directly to the vertices of the graph but to the elements of the filtered vertex sequence. - If the first positional argument is an integer, all remaining arguments are expected to be integers. They are considered as indices of the current vertex set again.
关键字参数可用于根据顶点的属性进行过滤。关键字的名称指定了属性的名称和过滤操作符,它们应该通过下划线(_)字符连接。属性名称也可以包含下划线,但操作符名称不包含,因此操作符始终是关键字名称中不包含下划线的最大尾部子字符串。可能的操作符有:
- eq: equal to
- ne: not equal to
- lt: less than
- gt: greater than
- le: less than or equal to
- ge: greater than or equal to
- in: checks if the value of an attribute is in a given list
- notin: checks if the value of an attribute is not in a given list
例如,如果你想过滤具有数值age属性大于200的顶点,你必须写:
>>> g.vs.select(age_gt=200) #doctest: +SKIP
同样地,要过滤其type在一组预定义类型列表中的顶点:
>>> list_of_types = ["HR", "Finance", "Management"] >>> g.vs.select(type_in=list_of_types) #doctest: +SKIP
如果省略操作符,则默认为eq。例如,以下选择器选择cluster属性等于2的顶点:
>>> g.vs.select(cluster=2) #doctest: +SKIP
在未知运算符的情况下,假定识别的运算符是属性名称的一部分,实际运算符为 eq。
从关键字参数推断出的属性名称如果以下划线开头(_),则会被特殊处理。这些不是真正的属性,而是指代顶点的特定属性,例如其度数。规则如下:如果属性名称以下划线开头,名称的其余部分将被解释为Graph对象的方法。该方法以顶点序列作为其第一个参数(其他参数保持默认值)调用,并根据该方法返回的值过滤顶点。例如,如果你想排除孤立的顶点:
>>> g = Graph.Famous("zachary") >>> non_isolated = g.vs.select(_degree_gt=0)
对于需要长时间计算的属性(例如,大型图的介数中心性),建议预先计算这些值并将其存储在图的属性中。当你在同一个select()调用中多次基于同一属性进行选择时,同样适用,以避免不必要地计算两次。例如,以下代码会计算两次介数中心性:
>>> edges = g.vs.select(_betweenness_gt=10, _betweenness_lt=30)建议使用以下替代方案:
>>> g.vs["bs"] = g.betweenness() >>> edges = g.vs.select(bs_gt=10, bs_lt=30)
| 返回 | |
| 新的、过滤后的顶点序列 |