Python 如何从字典中创建值的总列表
我目前有一个dict,我想从中创建一个最终的键和值的总列表:Python 如何从字典中创建值的总列表,python,list,dictionary,Python,List,Dictionary,我目前有一个dict,我想从中创建一个最终的键和值的总列表: adict = {'f': {'g', 'd'}, 'd': {'g'}, 'e': {'d'}, 'b': {'d'}, 'c': {'f', 'e'}, 'a': {'b', 'c'}} 我目前正在寻找以下格式的函数: def create_final_total_list(thedictionary: dict(), start
adict = {'f': {'g', 'd'},
'd': {'g'},
'e': {'d'},
'b': {'d'},
'c': {'f', 'e'},
'a': {'b', 'c'}}
我目前正在寻找以下格式的函数:
def create_final_total_list(thedictionary: dict(), startingkey:str):
final_list = []
# function here
我想要的是让用户输入一个起始键,将该键及其值附加到最终的_列表中。这些值也会变成键,这些键也会将它们的所有值附加到最终的_列表中,依此类推
示例:如果起始键为“a”,则它首先会:
final_list = ['a', 'b', 'c']
然后它会看到'b'和'c'值,并从dict中添加它们的值,这样它就会变成:
final_list = ['a', 'b', 'c',
'b', 'd',
'c', 'f', 'e', ...]
final_list = ['a', 'b', 'c',
'b', 'd',
'c', 'f', 'e'
'd', 'g'
'f', 'g', 'd'
'e', 'd' ...]
从‘d’、‘f’和‘e’的值来看,它将变成:
final_list = ['a', 'b', 'c',
'b', 'd',
'c', 'f', 'e', ...]
final_list = ['a', 'b', 'c',
'b', 'd',
'c', 'f', 'e'
'd', 'g'
'f', 'g', 'd'
'e', 'd' ...]
等等
它有点像一个到达函数,从一个键和它的值到达下一个键
在Python 3.3中,我将如何实现这一点?以下内容如何: 使用
deque
作为队列:
>>> from topsort import adict, create_final_total_list
>>> adict
{'a': {'b', 'c'}, 'b': {'d'}, 'c': {'e', 'f'}, 'd': {'g'}, 'e': {'d'}, 'f': {'d', 'g'}}
>>> create_final_total_list(adict, 'c')
['c', 'e', 'f', 'f', 'd', 'g', 'g', 'd', 'g', 'g', 'e', 'd', 'd', 'g', 'g']
该函数的代码为:
def create_final_total_list(the_dict, startingkey):
final_list = []
var = deque([startingkey])
while var:
u = var.pop()
final_list.append(u)
s = the_dict[u] if u in the_dict else None
if s:
final_list.extend(s)
var.extend(s)
return final_list
我修改了您的第一个字典,使其包含列表的dict(否则语法不正确),然后我将您的最终列表作为out参数传递:
>>> adict = {'f': ['g', 'd'],
'd': ['g'],
'e': ['d'],
'b': ['d'],
'c': ['f', 'e'],
'a': ['b', 'c']}
>>> def create_final_total_list(thedictionary, startingkey, final_list):
final_list.append(startingkey)
final_list.extend(thedictionary[startingkey])
>>> fl = []
>>> create_final_total_list(adict, 'a', fl)
>>> fl
['a', 'b', 'c']
>>> create_final_total_list(adict, 'b', fl)
>>> create_final_total_list(adict, 'c', fl)
>>> fl
['a', 'b', 'c', 'b', 'd', 'c', 'f', 'e']
>>>
我看到一个贴出的答案,所以我无论如何都会贴出我的答案。我的队列实现是简单的列表,但不确定使用Queue或dqueue是否有任何好处 实施
def create_final_total_list(thedictionary, key):
final_list = list(thedictionary[key])
Q = list(thedictionary[key])
while Q:
key = Q.pop(0)
final_list+=thedictionary.get(key, [])
Q+=thedictionary.get(key, [])
return final_list
输出
>>> create_final_total_list(adict, 'a')
['c', 'b', 'e', 'f', 'd', 'd', 'd', 'g', 'g', 'g', 'g']
递归实现可以是:
from itertools import chain
adict = {'f': {'g', 'd'},
'd': {'g'},
'e': {'d'},
'b': {'d'},
'c': {'f', 'e'},
'a': {'b', 'c'}}
def create_final_list(dict_, key):
values = list(dict_.get(key, []))
return [key] + values + \
list(chain(*[create_final_list(dict_, v) for v in values]))
print create_final_list(adict, "a")
这张照片是:
['a', 'c', 'b', 'c', 'e', 'f', 'e', 'd', 'd', 'g', 'g', 'f', 'd', 'g', 'd', 'g', 'g', 'g', 'b', 'd', 'd', 'g', 'g']
请注意,集合
{“a”,“b”}
中元素的顺序不是固定的,因此顺序可能会有所不同。我假设输入指令通过其相邻节点定义了一些有向图,对吗?我想这是拓扑排序。@bereal您的假设是正确的。我会想象一个递归解决方案是最短和最简单的(取决于你的世界观)最简单。希望我有一点时间把它们放在一起…也许今晚…{'g','d'}
是有效的语法,定义了一个集合