请问大家现在有这样三列数据:
| 节点i | 节点j |时间(秒)|
1 4 1254192988
3 4 1254194656
1 2 1254202612
25 1 1254232804
14 16 1254263166
1 16 1254271943
22 16 1254272423
1 2 1254273043
3 16 1254273303
2 1 1254274810
27 1 1254275440
表示用户i在时间 t 时评论了用户 j的答案
怎么用Python画出这个网络呢?
你可以使用 networkx 库来绘制这个网络,具体步骤如下:
导入必要的库
import networkx as nx
import matplotlib.pyplot as plt
创建有向图并添加节点和边
G = nx.DiGraph()
# 添加节点
for i in range(1, 28):
G.add_node(i)
# 添加边
edges = [(1, 4), (3, 4), (1, 2), (25, 1), (14, 16), (1, 16), (22, 16),
(1, 2), (3, 16), (2, 1), (27, 1)]
for edge in edges:
G.add_edge(edge[0], edge[1])
绘制网络
nx.draw(G, with_labels=True, font_weight='bold')
plt.show()
这里的 with_labels=True 表示绘制节点标签,font_weight='bold' 表示节点标签字体加粗。你可以根据需要进行调整,例如修改节点大小、修改边的颜色等。完整代码如下:
import networkx as nx
import matplotlib.pyplot as plt
# 创建有向图并添加节点和边
G = nx.DiGraph()
# 添加节点
for i in range(1, 28):
G.add_node(i)
# 添加边
edges = [(1, 4), (3, 4), (1, 2), (25, 1), (14, 16), (1, 16), (22, 16),
(1, 2), (3, 16), (2, 1), (27, 1)]
for edge in edges:
G.add_edge(edge[0], edge[1])
# 绘制网络
nx.draw(G, with_labels=True, font_weight='bold')
plt.show()
运行后,可以得到以下网络图:

不知道你这个问题是否已经解决, 如果还没有解决的话:import re
aString = 'this is a string where the substring "is" is repeated several times'
print([(a.start(), a.end()) for a in list(re.finditer('is', aString))])
[(2, 4), (5, 7), (38, 40), (42, 44)]