亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

圖元組列表之間的關系

圖元組列表之間的關系

呼喚遠方 2022-06-07 19:12:08
from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2from tensorflow.python.tools import optimize_for_inference_libloaded = tf.saved_model.load('models/mnist_test')infer = loaded.signatures['serving_default']f = tf.function(infer).get_concrete_function(                            flatten_input=tf.TensorSpec(shape=[None, 28, 28, 1],                                                         dtype=tf.float32)) # change this line for your own inputsf2 = convert_variables_to_constants_v2(f)graph_def = f2.graph.as_graph_def()if optimize :    # Remove NoOp nodes    for i in reversed(range(len(graph_def.node))):        if graph_def.node[i].op == 'NoOp':            del graph_def.node[i]    for node in graph_def.node:        for i in reversed(range(len(node.input))):            if node.input[i][0] == '^':                del node.input[i]    # Parse graph's inputs/outputs    graph_inputs = [x.name.rsplit(':')[0] for x in frozen_func.inputs]    graph_outputs = [x.name.rsplit(':')[0] for x in frozen_func.outputs]    graph_def = optimize_for_inference_lib.optimize_for_inference(graph_def,                                                                  graph_inputs,                                                                  graph_outputs,                                                                  tf.float32.as_datatype_enum)# Export frozen graphwith tf.io.gfile.GFile('optimized_graph.pb', 'wb') as f:    f.write(graph_def.SerializeToString())我有一個元組列表,比方說tuplist = [('a','b'),('a','c'),('e','f'),('f','c'),('d','z'),('z','x')]我正在嘗試獲得以下信息:('a','b','c','e','f'),('d','z','x')這是鏈接在一起的所有元素(就像圖論中的樹) 上述元組(也可以是列表)中的順序無關緊要。我已經設法獲得單個元素的所有鏈接的字典,但我正在努力以一種干凈有效的方式獲得最終結果......這是我到目前為止的代碼:ll=[('a','b'),('a','c'),('e','f'),('f','c'),('d','z'),('z','x')]total = {}total2={}final=[]for element in set([item for sublist in ll for item in sublist]):
查看完整描述

2 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

與這篇文章類似,您要查找的內容稱為圖的連接組件。一個簡單的方法是用 構建一個圖networkx,然后找到connected_components:


tuplist = [('a','b'),('a','c'),('e','f'),('f','c'),('d','z'),('z','x')]


import networkx as nx 


G=nx.Graph()

G.add_edges_from(tuplist)

list(nx.connected_components(G))

# [{'a', 'b', 'c', 'e', 'f'}, {'d', 'x', 'z'}]


查看完整回答
反對 回復 2022-06-07
?
慕后森

TA貢獻1802條經驗 獲得超5個贊

可選的遞歸解決方案,雖然不像@yatu的解決方案那樣優雅networkx:


tuplist = [('a','b'),('a','c'),('e','f'),('f','c'),('d','z'),('z','x')]

def get_graphs(start, c = [], seen = []):

  _r = [b for a, b in tuplist if a == start and b not in seen]+[a for a, b in tuplist if b == start and a not in seen]

  if _r:

     yield from [i for b in _r for i in get_graphs(b, c=c+[start, b, *_r], seen = seen+[start, b])]

  else:

     yield set(c)

     _r = [a for a, _ in tuplist if a not in seen]

     yield from ([] if not _r else get_graphs(_r[0], c = [], seen= seen))



result = list(get_graphs(tuplist[0][0]))

final_result = [a for i, a in enumerate(result) if all(all(k not in b for k in a) for b in result[i+1:])]

to_tuples = list(map(tuple, final_result))

輸出:


[('f', 'e', 'a', 'c', 'b'), ('z', 'd', 'x')]


查看完整回答
反對 回復 2022-06-07
  • 2 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號