![](/img/trans.png)
[英]Split dictionary items containing nested lists and map elements of one list
[英]Group the elements in a list of lists into a nested dictionary
我有以下列表示例列表(仅显示一部分):
[
["4YBB|1|AA|A|262", "4YBB|1|AA|A|263", "s35"],
["4YBB|1|AA|U|261", "4YBB|1|AA|A|263", "tSH",],
["4YBB|1|AA|U|261", "4YBB|1|AA|C|264", "ntSH", "s55"],
["4YBB|1|AA|G|259", "4YBB|1|AA|C|267", "cWW"],
["4WOI|1|DA|A|262", "4WOI|1|DA|A|263", "s35", "cWW"],
["4WOI|1|DA|C|264", "4WOI|1|DA|G|265", "s35"]
....
]
我想根据以下键列表将此列表中的元素分组为嵌套字典:
outer_key = ["4YBB|1|AA", "4WOI|1|DA"]
inner_key = [(259, 267), (259, 260), (260, 261), (260, 265), (260, 267), (261, 263), (261, 264), (262, 263), (264, 265), (265, 267)]
如您所见,外键表示内部列表的索引[0]和索引[1]的元素的子集,而内键元组值表示索引的[0]和索引[1]的最后一个元素使用“ |”分割时的内部列表 字符。 内部键元组表示位置(x,y)的所有可能组合,这些位置可能具有“交互作用”(内部列表的index [2]开始)。 因此,并非所有键都有与其关联的值。 如果不存在特定的内部元组键,则在其值后附加“-”。
pw_info = {
"4YBB|1|AA" : {
(259, 267): "cWW",
(259, 260): "-",
(260, 261): "-",
(260, 265): "-",
(260, 267): "-",
(261, 263): "tSH",
(261, 264): "ntSH;s55",
(262, 263): "s35",
(264, 265): "-",
(265, 267): "s35"
},
"4WOI|1|DA" : {
(259, 267): "-",
(259, 260): "-",
(260, 261): "-",
(260, 265): "-",
(260, 267): "-",
(261, 263): "-",
(261, 264): "-",
(262, 263): "s35;cWW",
(264, 265): "s35",
(265, 267): "-"
}
}
必须根据外部和内部密钥列表对密钥进行排序。 同样,内部列表可能包含3个以上的元素。 如果元素多于3个,则使用“;”将索引[2]和更高位置的元素连接在一起。 作为内部字典值(例如:(261,264):“ ntSH; s55”)。 做这个的最好方式是什么?
至于“ 键必须根据外部和内部键列表进行排序”-请记住,字典是无序的数据结构。
OrderedDict
对象是替代方法。
from collections import OrderedDict
import pprint
input_list = [
["4YBB|1|AA|A|262", "4YBB|1|AA|A|263", "s35"],
["4YBB|1|AA|U|261", "4YBB|1|AA|A|263", "tSH", ],
["4YBB|1|AA|U|261", "4YBB|1|AA|C|264", "ntSH", "s55"],
["4YBB|1|AA|G|259", "4YBB|1|AA|C|267", "cWW"],
["4WOI|1|DA|A|262", "4WOI|1|DA|A|263", "s35", "cWW"],
["4WOI|1|DA|C|264", "4WOI|1|DA|G|265", "s35"]
]
outer_keys = ["4YBB|1|AA", "4WOI|1|DA"]
inner_keys = [(259, 267), (259, 260), (260, 261), (260, 265), (260, 267),
(261, 263), (261, 264), (262, 263), (264, 265), (265, 267)]
# prepopulated dict indexed by `outer_keys` and
# containing OrderedDicts with default values for `inner_keys`
pw_info = {k: OrderedDict({t: '-' for t in inner_keys}) for k in outer_keys}
for sub_lst in input_list:
# extract starting slice from first 2 items (like `4YBB|1|AA`)
k0, k1 = sub_lst[0][:9], sub_lst[1][:9]
# check if 2 slices are equal and contained in `pw_info` dict (i.e. `outer_keys`)
if k0 == k1 and k0 in pw_info:
v1, v2 = sub_lst[0], sub_lst[1]
# `sub_key` is aimed to be a key for inner dict of the predefined `pw_info` dict
# thus it's composed as a tuple of trailing numbers of the first 2 items
# in sub_list (ex. `(262, 263)`)
sub_key = (int(v1[v1.rfind('|')+1:]), int(v2[v2.rfind('|')+1:]))
pw_info[k0][sub_key] = sub_lst[2] if len(sub_lst) == 3 else ';'.join(sub_lst[2:])
pprint.pprint(pw_info)
输出:
{'4WOI|1|DA': OrderedDict([((259, 267), '-'),
((259, 260), '-'),
((260, 261), '-'),
((260, 265), '-'),
((260, 267), '-'),
((261, 263), '-'),
((261, 264), '-'),
((262, 263), 's35;cWW'),
((264, 265), 's35'),
((265, 267), '-')]),
'4YBB|1|AA': OrderedDict([((259, 267), 'cWW'),
((259, 260), '-'),
((260, 261), '-'),
((260, 265), '-'),
((260, 267), '-'),
((261, 263), 'tSH'),
((261, 264), 'ntSH;s55'),
((262, 263), 's35'),
((264, 265), '-'),
((265, 267), '-')])}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.