【问题标题】:How to find top 10 scored nodes from a dictionary如何从字典中找到得分最高的 10 个节点
【发布时间】:2021-07-01 00:27:49
【问题描述】:

我已经找到了 4039 个节点的分数。但是我只需要排序/打印出前 10 个节点(根据最高分数)。

def between_parallel(G, processes = None):
    p = Pool(processes=processes)
    part_generator = 4*len(p._pool)
    node_partitions = list(partitions(G.nodes(), int(len(G)/part_generator)))
    num_partitions = len(node_partitions)
 
    bet_map = p.map(btwn_pool,
                        zip([G]*num_partitions,
                        [True]*num_partitions,
                        [None]*num_partitions,
                        node_partitions))
 
    bt_c = bet_map[0]
    for bt in bet_map[1:]:
        for n in bt:
            bt_c[n] += bt[n]
    return bt_c
bt = between_parallel(G_fb)

print (bt)

输出:

{0:0.1463059214744286,1:2.7832744209034606e-06,2:7.595021178512074e-08,3:1.6850656559280464e-06,4:1.8403320547933104e-07,5:2.205964164092193e-06,6:2.4537760730577472e-08, 7:0.0001702984836730361,8:2.7604980821899654e-07,9:1.6454236303026905e-05,10:4.986739552037655e-08,11:0.0,12:0.0,13:1.7622717578436846e-06,14:5.582871686568508e-07,15:0.0 ,16:1.9979459275532697e-07,17:4.1066669000480344e-07,18:0.0,19:5.062957964075819e-06,20:6.793693332142838e-07,21:0.0009380243844653207,22:6.703002200833232e-07,23:6.860348937590618e-06 ,24:1.3673472422981514e-07,25:5.38808313945586e-05,26:1.935436798204632e-05,27:3.067220091322184e-08,28:3.812160659244892e-07,29:1.3954817951917517e-06,30:1.3694627409316544e-06, 31:4.932641252790837E-06,32:0.0,33:0.0,34:0.003602081281963106,35:0.0,36:2.726417858953052O-08,37:0.0,38:7.344481172858350-07,39:9.548326289:9.548632620274015E-07,40:90 1.3796059569123122e-05, 41: 4.673274954378314e-06, 42: 0.0 ,43:0.0,44:0.0,45:3.0243457405507097E-07,46:0.0,47:0.0,48:5.29085361854899C-06,49:1.8403320547933104C-07,50:5.1354679522422770-07,51:9.201660273966552CE- 08,52:0.0,53:1.0849233385514972e-05,54:3.6120168027808387e-07,55:1.8160988132495756e-07,56:0.0010850657064421774,57:1.5359027152243021e-06,58:0.08436020590796543,59:7.274796871349583e-07,60 :7.546141619678459e-08,61:6.134440182644368e-08,62:2.8660815656610625e-06,63:0.0,64:1.2821395685548659e-05,65:6.893923913652614e-07,66:9.542251505382869e-08,67:0.0009118520705882034,68 :1.4722656438346484S-07,69:1.9840927984607338C-07,70:0.0,71:0.0,72:1.81251061308580430-06,73:6.016664242341218E-07,74:0.0,75:2.0,75:2.5490958649812190958/20,75:2.5490958640,070,75:2.0.0,75:2.54909586498580 77:2.556016742768486e-07,78:0.0,79:2.0870669696912043e-08,80:0.0001596017223936386,81:6.134440182644368e-08,82:7.3592283705503054e-06,83:9.201660273966552e-08,84:1.3929533620512854e-06, 85:5.032627527050142e-07、86:0.0、87:4.62586370 2493754e-05,88:4.549846123020853e-06,89:3.312597698627958e-07,90:0.0,91:2.7604980821899654e-07,92:6.842999005760918e-07,93:9.40614161338803e-08,94:2.1380836623092413e-06, 95:6.134440182644368e-08,96:7.835986401270893e-07,97:4.089626788429578e-08,98:6.75276474687329e-06,99:8.161726604908745e-07,100:5.316514824958451e-07,101:7.890545400009306e-07,102 :0.0,103:9.419040787733043e-05,104:3.0155346005494676e-06,105:4.083494447761187e-07,106:3.6387941210384125e-07,107:0.48051807855601425,108:3.481582493846834e-07,109:5.787658323854831e-06,110 :0.0,111:1.5294230468062708e-06,112:0.0,113:5.229334396770685e-06,114:0.0,115:3.3336831770046665e-06,116:1.155762168033134e-06,117:2.095933729070159e-07,118:5.694459451841912e -06,119:0.0015229358857942355,120:4.473603006040869e-05,121:1.0410849313922507e-06,122:0.0007457760472155037,123:4.922689779281791e-07,124:1.6358507153718312e-07,125:8.236283906591862e-06,126:1.68697105022720 1E-07,127:6.370611703673157E-07,128:2.4337383726394154E-06,129:4.49858946727259990 134:6.323857056647831e-07,135:0.0,136:0.02687035978739826,137:2.113265956887789e-06,138:0.0,139:1.2268880365288736e-08,140:9.16271144741008e-08,141:2.6854718971158224e-06,142:5.167781965397462 E-06,143:1.1515220571420997e-06,144:8.436846952494006e-07,145:0.0,146:0.00014135440728774116,147:2.4537760730577472e-08,148:2.4037945179865694e-06,149:3.6641108583001195e-07,150:9.524858913463773 E-05,151:3.4080223236913156e-08,152:1.8403320547933104e-07,153:0.0,154:0.0,155:4.089626788429578e-08,156:0.0001652742424942233,157:4.089626788429578e-08,158:4.792698586965206e-06 ,159:1.151331739012335e-06,160:0.0,161:2.4042212720527196e-06,162:5.842323983470826e-08,163:2.529053292567992e-05,164:1.7526971950412477e-08,165:3.3803819101741307e-07,166:4.367137177 6444425e-07,167:4.498589467272537e-08,168:3.1351705266406723e-07,169:0.00040220047873007524,170:1.2076646755280126e-05,171:0.015492407332440322,172:8.376867955345824e-06,173:0.0036310041038156615,174:4.089626788429578e-08 ,175:3.309676536636223e-06,176:3.2067729765384276e-07,177:3.111037521198216e-07,178:1.3712597154786128e-06,179:6.134440182644368e-08,180:9.112055842871576e-07,181:1.8403320547933104e-07, 182:4.089626788429578e-08,183:0.0,184:1.2725037854772063e-06,185:2.828334869845182e-06,186:8.069594875170074e-06,187:4.0825673136162185e-07,188:1.0395368746413048e-05,189:1.2821395685548659e -05,190:1.8403320547933104e-07,191:0.0,192:3.680664109586621e-07,193:1.8403320547933104e-07,194:4.3451620726611605e-07,195:0.0,196:9.79246528679504e-07,197:5.392921255516175e -07,198:0.0016628780885075156,29:9.4503759849139552-06,200:1.383389514169764E-05,20:0.0.0,202:7.655514235000151C-06,203:1.646847637793786276377937862O-05,204:2.7295 75812106239e-06,205:0.0,206:1.6358507153718312e-07,207:0.0,208:1.8607645913792854e-08,209:0.0,210:0.0,211:2.256933560691282e-06,212:8.259734640887314e-07,213: 0.0004681297730936059,214:1.87056608140777140,0.0.0.0,215:0.0,216:0.0,217:0.0,219:0.0,218:0.0,219:2.4537760,219:2.453776073057747055070:0.0,221:0.0,220:0.0,221:4.898257539778 -07,223:1.1271584511988172584 :1.52338597869001770-06,231:2.88657935896443630-06,232:1.3245464993391094C-06,233:0.0,234:0.0,235:3.987386187188394C-08,236:8.01595841612901C-06,237:9.2016602739665520-08,236/901660273960 :1.5156979955809631e-06,239:1.8449221399712053e-05,240:6.134440182644368e-08,241:0.0,242:3.4113860859848286e-06,243:4.675806628104484e-07,244:0.0,245:1.8403320547933104e-07,246 : 3.8221640264939854e-05, 247: 6.134440182644368e-0 8、248:2.118816268883605e-06、249:3.218483170457865e-06、250:2.1706480646280067e-08、251:3.939186945855204e-07、252:


Would anybody help me to show a way to print top 10 nodes.

Thanks

【问题讨论】:

  • 您可以使用计数器,而不是使用简单的字典。它提供了一个 most_common 方法来获取前 n 个条目。参考docs.python.org/3.1/library/…

标签: python social-networking


【解决方案1】:
# Get a list of key/value pairs from the dictionary.
data = [(k, v) for (k, v) in bt.items()]

# Sort the list by value in reverse order (biggest to smallest)
data.sort(key = lambda x: x[1], reverse=True)

# Get the top 10 as a dictionary.
top_10 = { k: v for (k, v) in data[:10] }

print(top_10)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多