我已經堅持了幾天的問題,任何人都可以提供提示,我很感激!
描述:
我有一本字典,但我想合并它的一些值和鍵,例如:
Input:
initial_dict = {'aa': ['AA'],'bb':['BB'],'BB':['MM'],'cc':['dd'],'dd':['GG','HH','LL']}
Desired output:
goal_dict = {'aa': ['AA'],'bb':['BB','MM'],'cc':['dd','GG','HH','LL']}
這意味著,如果一個鍵/值已經在它之前的鍵或值中,那么如果這個值/鍵還沒有存在,那么將它的值/鍵附加到之前的值上(可能不太清楚,但請參閱上面的輸入和輸出)。
我有以下代碼,我認為它們很完美,但是我得到了很多重復的輸出:
dict_head_conj_pair = {'aa': ['AA'],'bb':['BB'],'BB':['MM'],'cc':['dd'],'dd':['GG','HH','LL']}
dict_head_conj_new = {}
print(dict_head_conj_pair)
for a_key in dict_head_conj_pair.keys():
if dict_head_conj_new:
if a_key not in dict_head_conj_new.keys():
for idx_value, new_value_list in enumerate(dict_head_conj_new.copy().values()):
if a_key in new_value_list:
for idx_key, new_key in enumerate(dict_head_conj_new.copy().keys()):
if idx_key == idx_value:
target_key = new_key
for con_0 in range(len(dict_head_conj_pair[a_key])):
for new_value_list_1 in dict_head_conj_new.copy().values():
dict_head_conj_new[target_key].append(dict_head_conj_pair[a_key][con_0])
else:
for con_1 in range(len(dict_head_conj_pair[a_key])):
if con_1 == 0:
dict_head_conj_new.setdefault(a_key, []).append(dict_head_conj_pair[a_key][con_1])
else:
dict_head_conj_new[a_key].append(dict_head_conj_pair[a_key][con_1])
else:
for con_2 in range(len(dict_head_conj_pair[a_key])):
if con_2==0:
dict_head_conj_new.setdefault(a_key, []).append(dict_head_conj_pair[a_key][con_2])
else:
dict_head_conj_new[a_key].append(dict_head_conj_pair[a_key][con_2])
print("dict_head_conj_new: ",dict_head_conj_new)
當前不需要的輸出:
dict_head_conj_new: {'aa': ['AA'], 'bb': ['BB', 'MM', 'MM', 'MM'], 'BB': ['MM'], 'cc': ['dd', 'dd', 'dd', 'GG', 'GG', 'GG', 'GG', 'GG', 'HH', 'HH', 'HH', 'HH', 'HH', 'LL', 'LL', 'LL', 'LL', 'LL'], 'dd': ['GG', 'HH', 'LL', 'GG', 'HH', 'LL', 'GG', 'HH', 'LL']}
如果有人能看到我錯在哪里或提供有關如何獲得所需結果的提示,我將不勝感激!
謝謝!
uj5u.com熱心網友回復:
some_list = [1, 1, 1, 2, 3]
some_list = list(dict.fromkeys(some_list))
您可以運行除錯器來查找導致問題的原因(這可能很好),但是如果您只需要洗掉重復項,那么上面的代碼可能會起作用。(用于從串列中洗掉重復項)。
此外,我有一種感覺,圖論演算法可能與此處相關......(就像在字典中一樣,看看您是否可以使用鍵訪問元素,如果可以,您可以“蜘蛛進入”一個樹)
uj5u.com熱心網友回復:
如果我理解正確,您可以將其建模為圖形問題,因此我建議您使用 networkx:
import operator
import networkx as nx
initial_dict = {'aa': ['AA'], 'bb': ['BB'], 'BB': ['MM'], 'cc': ['dd'], 'dd': ['GG', 'HH', 'LL']}
dg = nx.convert.from_dict_of_lists(initial_dict, create_using=nx.DiGraph)
# these are the keys of the dictionaries
seeds = [n for n in dg.nodes if not dg.in_degree(n)]
def descendants(g, source):
"""This function finds all the descendants of source in g sorted by distance to source"""
desc = sorted(nx.shortest_path_length(g, source).items(), key=operator.itemgetter(1))
return [d for d, _ in desc if d != source]
# return as dictionary
res = {seed: descendants(dg, seed) for seed in seeds}
print(res)
輸出
{'aa': ['AA'], 'bb': ['BB', 'MM'], 'cc': ['dd', 'HH', 'GG', 'LL']}
uj5u.com熱心網友回復:
這適用于您的資料,因為它會產生您想要的輸出。雖然對于非常大的字典可能效率低下(因為需要搜索值),但肯定比原始問題中的代碼少得多。
initial_dict = {'aa': ['AA'],'bb':['BB'],'BB':['MM'],'cc':['dd'],'dd':['GG','HH','LL']}
goal_dict = dict()
def find_value(val):
for k, v in goal_dict.items():
if val in v:
return k
for k, v in initial_dict.items():
if (_k := find_value(k)):
goal_dict[_k] = v
elif not k in goal_dict:
goal_dict[k] = v
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329502.html
上一篇:Json.NET序列化Dictionary<TKey,TValue>與KeyValuePair<TKey,TValue>
