-2
{'a':[0,1], 'b':[1,2], 'c':[2,3], 'd':[10,11], 'e':[11,12], 'f':[12,13]}    

The goal is to find connected values in the dictionary. So, the result would be, order does not matter:

{1:['a','b','c'], 2:['d','e','f']}    

I've tried loop with many conditions and recursion. But got even more confused only.

Loop example, which works, but returns duplicates of connected values:

def used(a, data):
    try:
        for key in data.keys():
            if a in data[key]:
                return True
            else:
                return False
    except:
        return False

def is_connected(a_data, b_data):
    if [a for a in a_data if a in b_data]:
        return True
    else:
        return False

collection = {}
key = 1
get_init = True
for i in edges:
    for e in edges:
        if used(e, collection):
            continue
        if get_init:
            init = e
            chain = []
            chain.append(init)
            get_init = False
            continue
        else:
            pass
        if is_connected(init, e):
            chain.append(e)
            init = e
        else:
            continue
    collection[key] = chain
    key += 1
    get_init = True

Recursion, that doesn't work at all:

def recursion(a, data):
        for d in data:
            if is_connected(a, d):
                print d
                a = d
                recursion(a, data)
  • dictionaries are not ordered. https://stackoverflow.com/questions/6083531/order-of-keys-in-python-dictionary – J'e Aug 03 '17 at 12:33

1 Answers1

0

A recursion did the job. To achieve the result, I've changed a condition. And started with a list, not a dictionary.

def set_chains(name):
    region = {}
    edges_init = cmds.ls(sl=True, fl=True)
    edges_split = split_edges_on_chains(edges_init)

    for i, edges in edges_split.iteritems():
        cmds.select(edges)
        vtxs = cmds.ls(cmds.polyListComponentConversion(tv=True), fl=True)
        region[str(i)] = [v.split('.')[-1] for v in vtxs]

    self.edges[name] = region

def is_connected(a, b):
    a_vtxs = cmds.ls(cmds.polyListComponentConversion(a, tv=True), fl=True)
    b_vtxs = cmds.ls(cmds.polyListComponentConversion(b, tv=True), fl=True)
    shared_vtxs = [a for a in a_vtxs if a in b_vtxs]
    if shared_vtxs:
        return True
    else:
        return False

def get_edge_chain(chain, data):
    for c in chain:
        connected = [d for d in data if is_connected(c, d) and d not in chain]
        if not connected:
            continue
        chain.append(connected[0])
        data.remove(connected[0])
        return get_edge_chain(chain, data)
    del data[0]
    return chain

def split_edges_on_chains(data):    
    chains = {}
    iter = 0
    while data:
        chains[iter] = get_edge_chain([data[0]], data)
        iter += 1
    return chains

data = ['a','b','c','d','e','f']
split_edges_on_chains(data)

I find attributes of an every item of the data while calling a function

is_connected()