我需要计算图中每个顶点满足给定条件(例如“ACondition”)的次数。为此,我需要确保将顶点属性初始化为零,这是我明确执行的。请参阅下面的代码。
# Instantiates the graph object and the vertex property.
import graph_tool.all as gt
g1 = gt.Graph()
g1.vp.AProperty = g1.new_vertex_property("int32_t")
# Sets the vertex property to zero (prior to counting).
for v1 in g1.vertices():
g1.vp.AProperty[v1] = 0
# Counts the number of times "ACondition" is satisfied for each vertex.
for v1 in g1.vertices():
if(ACondition == True):
g1.vp.AProperty[v1] += 1
有没有办法指定属性的默认值,这样我就不需要显式设置它的初始值(即上面的第二个代码块)?