合并具有单个条目的字典

时间:2019-02-19 13:54:16

标签: python list dictionary

我有这个:

vscode.window.showTextDocument(entry.uri, {preview: true, preserveFocus: false})
    .then(() => {
        return vscode.commands.executeCommand('workbench.action.closeActiveEditor');
    });

有没有一种方法可以结合使用'descr'键来将两个(或更多)值保存在同一键下。

我想要这样的东西:

[[{'end': 3250741247,
   'ip_range': '193.194.64.0 - 193.194.95.255',
   'start': 3250733056},
  {'descr': 'PROVIDER'},
  {'descr': 'Algerian Academic Research Network'},
  {'country': 'DZ'}],
 [{'end': 3273187327,
   'ip_range': '195.24.192.0 - 195.24.223.255',
   'start': 3273179136},
  {'descr': 'Data communication and international'},
  {'descr': 'telecommunication of Cameroon'},
  {'country': 'CM'}]]

这可能吗?如果可以,怎么办?

我已经尝试过了,但是没有用:

[[{'end': 3250741247,
   'ip_range': '193.194.64.0 - 193.194.95.255',
   'start': 3250733056},
  {'descr': 'PROVIDER','Algerian Academic Research Network'},
  {'country': 'DZ'}],
 [{'end': 3273187327,
   'ip_range': '195.24.192.0 - 195.24.223.255',
   'start': 3273179136},
  {'descr': 'Data communication and international','telecommunication of Cameroon'},
  {'country': 'CM'}]]

2 个答案:

答案 0 :(得分:0)

将其保持在您所拥有的位置附近,对于您的示例,我会做类似的事情:

lists = [[{'end': 3250741247,
           'ip_range': '193.194.64.0 - 193.194.95.255',
           'start': 3250733056},
          {'descr': 'PROVIDER'},
          {'descr': 'Algerian Academic Research Network'},
          {'country': 'DZ'}],
         [{'end': 3273187327,
           'ip_range': '195.24.192.0 - 195.24.223.255',
           'start': 3273179136},
          {'descr': 'Data communication and international'},
          {'descr': 'telecommunication of Cameroon'},
          {'country': 'CM'}]
         ]
result = []
for list in lists:
    result.append({})
    for d in list:
        for key in d:
            if key in result[-1]: result[-1][key] += d[key]
            else                : result[-1][key]  = d[key]
print(result)

屈服:

[{'end': 3250741247, 'ip_range': '193.194.64.0 - 193.194.95.255', 'start': 3250733056, 'descr': 'PROVIDERAlgerian Academic Research Network', 'country': 'DZ'}, {'end': 3273187327, 'ip_range': '195.24.192.0 - 195.24.223.255', 'start': 3273179136, 'descr': 'Data communication and internationaltelecommunication of Cameroon', 'country': 'CM'}]

请注意,我在结果中将单独的词典合并为一个词典。另外,您可能想在组合中做一些更高级的事情,而不是像+=这样的+= (" " + d[key])来添加空格,但这会在非字符串上中断。您如何改善它取决于您。

答案 1 :(得分:0)

lst = [[{'end': 3250741247,
   'ip_range': '193.194.64.0 - 193.194.95.255',
   'start': 3250733056},
  {'descr': 'PROVIDER'},
  {'descr': 'Algerian Academic Research Network'},
  {'country': 'DZ'}],
 [{'end': 3273187327,
   'ip_range': '195.24.192.0 - 195.24.223.255',
   'start': 3273179136},
  {'descr': 'Data communication and international'},
  {'descr': 'telecommunication of Cameroon'},
  {'country': 'CM'}]]
# container for output list
new_list = []

# Loop through all the nested lists
for ls in lst:
    # container to capture the value of the 'descr' key
    descrval = []
    # container to capture the index of the dict in nested list
    descridx = []
    # now we loop through all the dicts in nested lists
    for i, dct in enumerate(ls):
        # if the 'descr' key exists, we capture it
        if 'descr' in dct.keys():
            descrval.append(dct['descr'])  # value
            descidx.append(i)  # nested list index
    # remove all existing dicts
    for i in sorted(descridx, ascending=False):
        ls.pop(i)
    # pops back the joint dict for the 'descr' key
    ls.append({'descr':descrval})
    # pushes the new list to the output list
    new_list.append(ls)

注意:我仅在文本框中构造了它,没有对其进行任何测试。请亲自检查正确性。