![](/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.