图元组列表之间的关系

from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2

from tensorflow.python.tools import optimize_for_inference_lib


loaded = 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 inputs

f2 = 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 graph

with 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]):



呼唤远方
浏览 130回答 2
2回答

互换的青春

与这篇文章类似,您要查找的内容称为图的连接组件。一个简单的方法是用 构建一个图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'}]

慕后森

可选的递归解决方案,虽然不像@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')]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python