Python igraph : delete vertices from a graph

标签 python graph igraph graph-modelling-language

我正在使用 enron 电子邮件数据集,我正在尝试删除没有“@enron.com”的电子邮件地址(即我只想使用 enron 电子邮件)。当我试图删除那些没有@enron.com 的地址时,一些电子邮件由于某些原因被跳过了。下面显示了一个小图,其中顶点是电子邮件地址。这是 gml 格式:

Creator "igraph version 0.7 Sun Mar 29 20:15:45 2015"
Version 1
graph
[
  directed 1
  node
  [
    id 0
    label "csutter@enron.com"
  ]
  node
  [
    id 1
    label "steve_williams@eogresources.com"
  ]
  node
  [
    id 2
    label "kutner.stephen@enron.com"
  ]
  node
  [
    id 3
    label "igsinc@ix.netcom"
  ]
  node
  [
    id 4
    label "dbn@felesky.com"
  ]
  node
  [
    id 5
    label "cheryltd@tbardranch.com"
  ]
  node
  [
    id 6
    label "slover.eric@enron.com"
  ]
  node
  [
    id 7
    label "alkeister@yahoo.com"
  ]
  node
  [
    id 8
    label "econnors@mail.mainland.cc.tx.us"
  ]
  node
  [
    id 9
    label "jafry@hotmail.com"
  ]
  edge
  [
    source 5
    target 5
    weight 1
  ]
]

我的代码是:

G = ig.read("enron_email_filtered.gml")
for v in G.vs:
    print v['label']
    if '@enron.com' not in v['label']:
        G.delete_vertices(v.index)
        print 'Deleted'

在此数据集中,应删除 7 封电子邮件。但是,根据上述代码,仅删除了 5 封电子邮件。

最佳答案

来自教程here ,您可以访问具有特定属性的所有顶点,然后按如下方式批量删除它们:

to_delete_ids = [v.index for v in G.vs if '@enron.com' not in v['label']]
G.delete_vertices(to_delete_ids)

这是我得到的输出:

to delete ids: [1, 3, 4, 5, 7, 8, 9]
Before deletion: IGRAPH D-W- 10 1 --
+ attr: id (v), label (v), weight (e)
+ edges:
5->5
After deletion: IGRAPH D-W- 3 0 --
+ attr: id (v), label (v), weight (e)
label: csutter@enron.com
label: kutner.stephen@enron.com
label: slover.eric@enron.com

关于Python igraph : delete vertices from a graph,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29332220/

相关文章:

python - 如何使用 Python 在 Mac OS X 的前台启动应用程序?

python - 从 `bokeh` 函数外部设置 python `figure()` 绘图的标题

python - 如何使用 Boost::Python 公开原始字节缓冲区?

python - 在 python pandas 中将多个年份列转换为单个年份列(整洁格式)

algorithm - 查找具有 x 个红色顶点的 2 个顶点之间的路径

r - igraph 或 ggnet2 的水平 TreeMap

python - 使用 Python 将数据发布到 Microsoft Graph

RStudio 增加条形图图形中的字体大小

c - 我如何找到 igraph 的最短路径的底层 C 代码?

r - 在 R(iGraph 等)中进行聚类后,您可以维护集群中的节点+边来进行单独的集群分析吗?