Python-基于匹配列表和嵌套集提取字典值

Python-基于匹配列表和嵌套集提取字典值,python,pandas,list,dictionary,key,Python,Pandas,List,Dictionary,Key,我正在努力获得我想要的输出。我有一份清单和一本字典,如下所示: h3_rules = [[{'LA'}, {'KE'}, 0.9468308801441875], [{'KE'}, {'LA'}, 0.9650949173300674], [{'EH'}, {'LA', 'KE'}, 0.9136231884057971], [{'LA'}, {'EH', 'KE'}, 0.9468308801441875], [{'KE'}, {'LA', 'EH'}, 0.9650949173300674]

我正在努力获得我想要的输出。我有一份清单和一本字典,如下所示:

h3_rules = [[{'LA'}, {'KE'}, 0.9468308801441875], [{'KE'}, {'LA'}, 0.9650949173300674], [{'EH'}, {'LA', 'KE'}, 0.9136231884057971], [{'LA'}, {'EH', 'KE'}, 0.9468308801441875], [{'KE'}, {'LA', 'EH'}, 0.9650949173300674], [{'LA', 'EH'}, {'KE'}, 0.9471153846153846], [{'EH', 'KE'}, {'LA'}, 0.9650949173300674], [{'LA', 'KE'}, {'EH'}, 1.0], [{'EH'}, {'KE'}, 0.9466666666666667], [{'KE'}, {'EH'}, 1.0], [{'LA'}, {'EH'}, 0.99969960949234], [{'EH'}, {'LA'}, 0.9646376811594203]]

and

h3_S = {'EH': [0.33326893353941267], 'KE': [0.31549459041731065], 'LA': [0.321580370942813], 'PR': [0.029656105100463678]}

如果嵌套列表的第一个索引与字典键匹配,我想向嵌套列表追加一个值。我要追加的值是通过将嵌套列表的最后一个索引除以相应字典键的值来计算的。下面是我希望通过使用第一个嵌套列表作为示例实现的手写示例。嵌套列表的第一个索引设置为“LA”:

目前我有下面的代码,但我一直在检查列表值是否与键匹配,并使用该值进行除法

# For every rule in rules
for i in h3_rules:
  # Check if the first index for every rule matches a dictionary key
  if h3_rules[i][0] == ?:
    # If there is a match append the newly calculated value based on the key value to the list
    h3_rules[i].append(h3_rules[i][-1] / ?)
  print(h3_rules) 
for h in h3_rules:
    first_element = next(iter(h[0])) if isinstance(h[0], set) else None
    if first_element is not None: h.append(h[-1]/h3_S[first_element][0])
    

我已经在Stack overflow中进行了广泛的查找,但没有找到任何内容。有没有关于如何实现理想结果的想法?

根据定义
h3_规则的方式,首先需要验证第一个索引是否是
集合的实例,获取集合的第一个值,然后添加相应的除法

# For every rule in rules
for i in h3_rules:
  # Check if the first index for every rule matches a dictionary key
  if h3_rules[i][0] == ?:
    # If there is a match append the newly calculated value based on the key value to the list
    h3_rules[i].append(h3_rules[i][-1] / ?)
  print(h3_rules) 
for h in h3_rules:
    first_element = next(iter(h[0])) if isinstance(h[0], set) else None
    if first_element is not None: h.append(h[-1]/h3_S[first_element][0])