繁体   English   中英

使用python从字典中提取客户端服务器的顺序

[英]Extracting order of client server from a dictionary using python

我有一个像下面这样的字典,例如,对于第一个项目,意味着5是2的客户。在最后一个项目中,如您所见,2是4的客户,而4也是项目3的客户。

customer_provider_dic= {'2':['5'],'1':['2'],'3':['4'],'4':['2']}

我正在尝试提取这些项目的所有客户链。 输出将如下所示:

[2,5]
[1,2,5]
[3,4,2,5]
[4,2,5]

我真的很困惑如何提取这些链。 关于流程图或步骤的任何建议。

首先,如果所有列表中都包含一个项目,那么这是一个可行的解决方案。

def simple_chain(graph, key):
    chain = [key]
    while True:
        lst = graph.get(key)
        if lst is None:
            # There's nothing left to add to the chain
            break
        key = lst[0]
        if key in chain:
            # We've found a loop
            break
        chain.append(key)
    return chain

customer_provider = {
    '2': ['5'], '1': ['2'], '3': ['4'], '4': ['2'],
}

data = customer_provider
for k in data:
    print(simple_chain(data, k))

输出

['2', '5']
['1', '2', '5']
['3', '4', '2', '5']
['4', '2', '5']

一般的解决方案要难一些。 我们可以使用递归生成器创建所有链。 下面的代码可以工作,但是效率不高,因为它多次创建相同的子链。

基本策略与以前的版本相似,但是我们需要遍历每个列表中的所有键,并为每个列表创建新的链。

要完全理解此代码的工作方式,您需要熟悉递归和Python 生成器 您可能还会发现此页面有帮助: 了解Python中的生成器 在线上也有各种Python生成器教程。

def make_chains(graph, key, chain):
    ''' Generate all chains in graph that start at key.
        Stop a chain when graph[key] doesn't exist, or if
        a loop is encountered.
    '''
    lst = graph.get(key)
    if lst is None:
        yield chain
        return
    for k in lst:
        if k in chain:
            # End this chain here because there's a loop
            yield chain
        else:
            # Add k to the end of this chain and
            # recurse to continue the chain
            yield from make_chains(graph, k, chain + [k])

customer_provider = {
    '2': ['5'], '1': ['2'], '3': ['4'], '4': ['2'],
}

pm_data = {
    '2': ['5'], '1': ['2'], '3': ['4', '6'], 
    '4': ['2'], '6': ['1', '5'],
}

#data = customer_provider
data = pm_data

for k in data:
    for chain in make_chains(data, k, [k]):
        print(chain)

如果我们使用data = customer_provider运行该代码,它将产生与先前版本相同的输出。 这是使用data = pm_data运行时的输出。

['2', '5']
['1', '2', '5']
['3', '4', '2', '5']
['3', '6', '1', '2', '5']
['3', '6', '5']
['4', '2', '5']
['6', '1', '2', '5']
['6', '5']

语法的yield from是Python 3的功能。 要在Python 2上运行此代码,请更改

yield from make_chains(graph, k, chain + [k])

for ch in make_chains(graph, k, chain + [k]):
    yield ch 

之前PYTHON 3.6 dict不保留键的插入顺序,所以for k in data可以在的键环data以任何顺序。 输出列表仍然是正确的。 您可能希望将该循环替换为

for k in sorted(data):

使链条井然有序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM