SetProperty[{obj,itemspec},namevalue]
设置 obj 中 itemspec 的属性 namevalue.
SetProperty
SetProperty[{obj,itemspec},namevalue]
设置 obj 中 itemspec 的属性 namevalue.
更多信息
- 属性用于存储和操作诸如 Graph 的对象内特定名称的项目的 name->value 对.
- SetProperty 将产生一个具有修改的属性值的对象.
- 项目指定 itemspec 通常具有格式 {collection,index}.
- index 通常可以有下列格式:
-
ind 单个索引 {ind1,ind2,…} 索引列表 All collection 中所有可能索引 patt collection 中匹配 patt 的索引 - collection 通常可以有下列格式:
-
col 单个集合 {col1,col2,…} 集合列表 All 所有可能集合 patt 匹配 patt - 格式为 collection 的项目指定 itemspec 指的是集合中所有索引的默认值.
- Graph 对象可用的项目包括顶点和边.
范例
打开所有单元 关闭所有单元基本范例 (2)
g = CompleteGraph[4]{SetProperty[{g, 1}, VertexLabels -> "one"], SetProperty[{g, 12}, EdgeStyle -> Brown]}CompleteGraph[4, VertexSize -> Small]SetProperty[{%, 1}, VertexStyle -> Red]SetProperty[{%, 1}, VertexStyle -> Blue]范围 (6)
基本用途 (2)
SetProperty[{[image], 1}, VertexSize -> Small]SetProperty[{[image], 12}, EdgeLabels -> "hello"]SetProperty[{[image], 1}, {VertexStyle -> Magenta, VertexLabels -> "hello"}]SetProperty[{[image], 12}, {EdgeStyle -> Magenta, EdgeLabels -> "hello"}]顶点属性 (2)
标准顶点属性包括 VertexStyle:
g = CompleteGraph[4, VertexSize -> Medium]SetProperty[{g, 4}, VertexStyle -> Red]SetProperty[{g, 1}, VertexLabels -> "one"]SetProperty[{g, 1}, VertexSize -> Tiny]SetProperty[{g, 1}, VertexShape -> [image]]SetProperty[{g, 2}, VertexShapeFunction -> "Square"]非可视化属性包括 VertexWeight:
SetProperty[{g, 1}, VertexWeight -> 100]PropertyValue[{%, 1}, VertexWeight]SetProperty[{[image], 2}, "Material" -> "Wood"]PropertyValue[{%, 2}, "Material"]边属性 (2)
标准边属性包括 EdgeStyle:
g = CompleteGraph[4]SetProperty[{g, 12}, EdgeStyle -> Brown]SetProperty[{g, 12}, EdgeLabels -> 12]SetProperty[{g, 34}, EdgeShapeFunction -> "HalfFilledDoubleArrow"]非可视化属性包括 EdgeWeight:
SetProperty[{g, 12}, EdgeWeight -> 42]PropertyValue[{%, 12}, EdgeWeight]SetProperty[{[image], 24}, "Resistance" -> 50.]PropertyValue[{%, 24}, "Resistance"]应用 (3)
使用 Fold 重复应用 SetProperty:
g = CycleGraph[8, VertexSize -> Large]Fold[SetProperty[{#1, #2}, VertexStyle -> ColorData[45, #2]]&, g, Range[1, 5]]FoldList[SetProperty[{#1, #2}, VertexStyle -> ColorData[45, #2]]&, g, Range[1, 6]]g = CompleteGraph[4]SetProperty[{g, 1}, VertexSize -> Medium]SetProperty[{g, 2}, VertexSize -> Medium]g = CompleteKaryTree[4, 3, DirectedEdges -> True]ListAnimate[Reap[BreadthFirstScan[g, {"FrontierEdge" -> (Sow[SetProperty[{g, #}, EdgeStyle -> Red]]& )}]][[2, 1]], DefaultDuration -> Length[EdgeList[g]] / 2, SaveDefinitions -> True]属性和关系 (6)
使用 Property 作为一个封装来指定诸如 Graph 的函数中的属性:
Graph[{Property[1, {VertexSize -> Medium, VertexStyle -> Red}], 2, 3}, {12, 23, 31}]使用 PropertyValue 来提取值:
g = CompleteGraph[4, VertexStyle -> {1 -> Red}, VertexSize -> Small]PropertyValue[{g, 1}, VertexStyle]对于未定义属性,返回数值 $Failed:
PropertyValue[{g, 1}, "foo"]MemberQ[PropertyList[{g, 1}], "foo"]使用 PropertyValue 来设置一个图中的属性:
g = CompleteGraph[4]PropertyValue[{g, 1}, VertexSize] = Small;g使用 PropertyValue 复原一个图中的属性:
g = CompleteGraph[4, VertexSize -> {1 -> Small}]PropertyValue[{g, 1}, VertexSize]=.;g使用 PropertyList 来测试属性的出现:
g = CompleteGraph[4, VertexLabels -> {1 -> one, 4 -> four}]MemberQ[PropertyList[{g, 1}], VertexLabels]Cases[VertexList[g], v_ /; MemberQ[PropertyList[{g, v}], VertexLabels] :> v -> PropertyValue[{g, v}, VertexLabels]]使用 RemoveProperty 在不改变原图的情况下,删除一个属性:
g = CompleteGraph[4, VertexStyle -> {1 -> Red}, VertexSize -> Small]RemoveProperty[{g, 1}, VertexStyle]g可能存在的问题 (1)
PropertyValue 通常比 SetProperty 更快:
g = CycleGraph[1000];Timing@Do[PropertyValue[{g, v}, VertexWeight] = RandomReal[], {v, VertexList[g]}]g = CycleGraph[1000];Timing@Do[g = SetProperty[{g, v}, VertexWeight -> RandomReal[]], {v, VertexList[g]}]文本
Wolfram Research (2010),SetProperty,Wolfram 语言函数,https://reference.wolfram.com/language/ref/SetProperty.html.
CMS
Wolfram 语言. 2010. "SetProperty." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/SetProperty.html.
APA
Wolfram 语言. (2010). SetProperty. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/SetProperty.html 年
BibTeX
@misc{reference.wolfram_2026_setproperty, author="Wolfram Research", title="{SetProperty}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/SetProperty.html}", note=[Accessed: 26-October-2026]}
BibLaTeX
@online{reference.wolfram_2026_setproperty, organization={Wolfram Research}, title={SetProperty}, year={2010}, url={https://reference.wolfram.com/language/ref/SetProperty.html}, note=[Accessed: 26-October-2026]}