networkx库绘制带权图给无权图加权重输出

作者:zheng____ 时间:2022-01-29 05:54:23 

问题

最近在研究图学习,在用networkx库绘图的时候发现问题。

'''
author:zheng
time:2020.10.23
'''
import networkx as nx
import random
g = nx.karate_club_graph()  # 空手道俱乐部
for u,v in g.edges:
   print(u,v)
   g.add_edge(u, v, weight=random.uniform(0, 1))  # 权值为(0,1)间的随机数
print(g.edges())

输出结果

[(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 10), (0, 11), (0, 12), (0, 13), (0, 17), (0, 19), (0, 21), (0, 31), (1, 2), (1, 3), (1, 7), (1, 13), (1, 17), (1, 19), (1, 21), (1, 30), (2, 3), (2, 7), (2, 8), (2, 9), (2, 13), (2, 27), (2, 28), (2, 32), (3, 7), (3, 12), (3, 13), (4, 6), (4, 10), (5, 6), (5, 10), (5, 16), (6, 16), (8, 30), (8, 32), (8, 33), (13, 33), (19, 33), (31, 24), (31, 25), (31, 28), (31, 32), (31, 33), (30, 32), (30, 33), (9, 33), (27, 23), (27, 24), (27, 33), (28, 33), (32, 14), (32, 15), (32, 18), (32, 20), (32, 22), (32, 23), (32, 29), (32, 33), (33, 14), (33, 15), (33, 18), (33, 20), (33, 22), (33, 23), (33, 26), (33, 29), (23, 25), (23, 29), (25, 24), (29, 26)]

发现了问题,我明明通过random.uniform(0, 1)随机设置了权重为什么在结果输出中并未显示,是输入权重的问题,还是结果展示的问题。

'''
author:zheng
time:2020.10.23
'''
import networkx as nx
import random
g = nx.karate_club_graph()  # 空手道俱乐部
for u,v in g.edges:
   g.add_edge(u, v, weight=random.uniform(0, 1))  # 权值为(0,1)间的随机数
print(g.edges(data=True))

大家看看两个代码有没有什么不同,在中添加了

此时输出结果:

[(0, 1, {'weight': 0.49899129531032826}), (0, 2, {'weight': 0.7493395367183026}), (0, 3, {'weight': 0.9805046801748599}), (0, 4, {'weight': 0.644560549909913}), (0, 5, {'weight': 0.022461095194206915}), (0, 6, {'weight': 0.39855273941801683}), (0, 7, {'weight': 0.9167666610641618}), (0, 8, {'weight': 0.3736839965822629}), (0, 10, {'weight': 0.1685687039463848}), (0, 11, {'weight': 0.5900599708379352}), (0, 12, {'weight': 0.49772285717726605}), (0, 13, {'weight': 0.6988903320924684}), (0, 17, {'weight': 0.8108991409995218}), (0, 19, {'weight': 0.21743421569163335}), (0, 21, {'weight': 0.687637570308398}), (0, 31, {'weight': 0.13180440967486262}), (1, 2, {'weight': 0.0603379086168323}), (1, 3, {'weight': 0.9536653778354264}), (1, 7, {'weight': 0.1680232359702576}), (1, 13, {'weight': 0.23821372652905115}), (1, 17, {'weight': 0.6861169007257469}), (1, 19, {'weight': 0.006553274592374314}), (1, 21, {'weight': 0.23452495215883118}), (1, 30, {'weight': 0.7638165639559286}), (2, 3, {'weight': 0.18381620307197954}), (2, 7, {'weight': 0.08671998389998026}), (2, 8, {'weight': 0.7395899045684956}), (2, 9, {'weight': 0.5973616237830935}), (2, 13, {'weight': 0.25253256663029156}), (2, 27, {'weight': 0.4151629971620948}), (2, 28, {'weight': 0.6830413630275037}), (2, 32, {'weight': 0.10877354662752325}), (3, 7, {'weight': 0.3165078261209674}), (3, 12, {'weight': 0.3258985972202395}), (3, 13, {'weight': 0.5617183737707032}), (4, 6, {'weight': 0.9944831897451706}), (4, 10, {'weight': 0.4258447405573552}), (5, 6, {'weight': 0.17102663345956715}), (5, 10, {'weight': 0.41020894392823837}), (5, 16, {'weight': 0.24048864347638477}), (6, 16, {'weight': 0.5401785263069063}), (8, 30, {'weight': 0.4604358340149278}), (8, 32, {'weight': 0.9601569527970788}), (8, 33, {'weight': 0.2905405465193912}), (13, 33, {'weight': 0.2556445407164615}), (19, 33, {'weight': 0.3008126988319231}), (31, 24, {'weight': 0.8781944129721222}), (31, 25, {'weight': 0.392828914742127}), (31, 28, {'weight': 0.7410701847068474}), (31, 32, {'weight': 0.39869250595380246}), (31, 33, {'weight': 0.4380052794486696}), (30, 32, {'weight': 0.4587792580500568}), (30, 33, {'weight': 0.5106934704075864}), (9, 33, {'weight': 0.9037424067215868}), (27, 23, {'weight': 0.9151325306454512}), (27, 24, {'weight': 0.6079907996445639}), (27, 33, {'weight': 0.6168782680542676}), (28, 33, {'weight': 0.9529880704286767}), (32, 14, {'weight': 0.21711370788129514}), (32, 15, {'weight': 0.21906480255644156}), (32, 18, {'weight': 0.36297161231472697}), (32, 20, {'weight': 0.8295507296873654}), (32, 22, {'weight': 0.725850047579389}), (32, 23, {'weight': 0.06395474428944792}), (32, 29, {'weight': 0.021001018687274553}), (32, 33, {'weight': 0.29227780907194645}), (33, 14, {'weight': 0.7898337840851372}), (33, 15, {'weight': 0.06574640956244104}), (33, 18, {'weight': 0.3193055980182168}), (33, 20, {'weight': 0.22814267912232755}), (33, 22, {'weight': 0.934928086748862}), (33, 23, {'weight': 0.8780586608909188}), (33, 26, {'weight': 0.834765093283264}), (33, 29, {'weight': 0.8927802653939352}), (23, 25, {'weight': 0.18106036608743914}), (23, 29, {'weight': 0.7824721548411848}), (25, 24, {'weight': 0.9362577071184671}), (29, 26, {'weight': 0.06557785001633887})]

如何只输出权重

import networkx as nx
import random
g = nx.karate_club_graph()  # 空手道俱乐部
for u,v in g.edges:
   g.add_edge(u, v, weight=random.uniform(0, 1))  # 权值为(0,1)
for (u,v,d) in g.edges(data=True):
   print(d['weight'])

输出结果

0.9175521740544361
0.09841104142600388
0.9557658899707079
0.9256010898041206
0.2519120041349847
0.48370396192288767
0.8354304958648846
0.758094795660556
0.7910256982243447
0.6281003207621544
0.9801420646231339
0.7941450155753779
0.3851720075568309
0.802202234860892
0.7923045754263267
0.5270583359776736
0.9523963539542339
0.7474601472346581
0.044707615637251674
0.5349188097983026
0.6158693844408302
0.9456154478628968
0.7547788968185274
0.5648525235741113
0.6657063624514532
0.3109915743055601
0.3969190047820317
0.8763009836310122
0.7101598558464499
0.012225959063178693
0.700579386399397
0.8304116006624506
0.426518724548162
0.07244870577629914
0.36116795615537345
0.45781457416039606
0.25726914791707645
0.29778955309109023
0.8892096639219873
0.39322230058450647
0.5085017515323529
0.9597980742524421
0.08034618164792517
0.9143712112937563
0.17242150180445381
0.8914706349104955
0.8480034205451665
0.8217034225251223
0.45552196009659873
0.3909280195122691
0.45119988941609357
0.02984583822414133
0.14404544949710196
0.45459370924953857
0.10296953351890004
0.4948127850493056
0.9238669854480596
0.9399144983422378
0.919211279645529
0.24084759450828674
0.4410486851096309
0.7699702465967465
0.27749525807367836
0.9449097003790671
0.5019309896062647
0.42774455164796255
0.43988066338230847
0.7405733579782761
0.2308870299365694
0.12306785713306911
0.7139426386075743
0.2640769424119722
0.031149630992576394
0.07700734539599274
0.37034537464573547
0.7034898163898959
0.8557141929947621
0.06539918397508715

来源:https://blog.csdn.net/zheng____/article/details/109812933

标签:networkx,带权图,无权图,权重输出
0
投稿

猜你喜欢

  • 微信小程序实现渐入渐出动画效果

    2024-04-23 09:32:53
  • Keras—embedding嵌入层的用法详解

    2021-06-05 01:08:17
  • python 寻找list中最大元素对应的索引方法

    2021-02-16 07:37:52
  • python3实现从kafka获取数据,并解析为json格式,写入到mysql中

    2023-06-12 21:30:21
  • Vue3通过ref操作Dom元素及hooks的使用方法

    2024-04-27 16:07:32
  • 解决pycharm界面不能显示中文的问题

    2023-04-08 07:14:03
  • 浅析MySQL数据库授权原则

    2009-12-15 09:21:00
  • 一文弄懂MySQL中redo log与binlog的区别

    2024-01-12 22:20:37
  • Django初步使用Celery处理耗时任务和定时任务问题

    2023-10-15 07:41:59
  • js模仿php中strtotime()与date()函数实现方法

    2024-04-18 10:01:55
  • Python中统计函数运行耗时的方法

    2023-03-11 02:17:25
  • MySQL创建用户与授权方法

    2024-01-19 02:43:55
  • php以post形式发送xml的方法

    2023-11-22 12:40:47
  • 在ASP中使用SQL语句之11:记录统计

    2007-08-11 13:27:00
  • 如何在mac下配置python虚拟环境

    2023-06-16 18:09:37
  • Python递归函数 二分查找算法实现解析

    2021-06-22 05:58:38
  • Python OpenCV学习之图像形态学

    2022-01-16 21:48:29
  • Python数据结构栈实现进制转换简单示例

    2022-10-21 13:48:21
  • asp无限级分类加js收缩伸展功能代码

    2009-12-08 12:25:00
  • Python二叉搜索树与双向链表转换算法示例

    2023-07-17 22:49:42
  • asp之家 网络编程 m.aspxhome.com