I have the following sample list of lists (only a section is shown):
[
["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"]
....
]
I would like to group the elements in this list into a nested dictionary based on the following list of keys:
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)]
As you can notice, the outer key represents a subset of the element at index [0] and index [1] of the inner list whereas the inner key tuple values represent the last element of the index [0] and index [1] of the inner list when split using the '|' character. The inner key tuples represent all the possible combination of positions (x,y) that might have an 'interaction' (index[2] onwards of the inner list). As such, not all keys will have a value associated with it. If a particular inner tuple key is not present, append the "-" to its value.
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): "-"
}
}
The keys must be ordered according to the outer and the inner key lists. Also, it is possible for the inner list to have more than 3 elements. If there are more than 3 elements, concatenate element at index [2] and higher together using ";" as the inner dictionary value (for example: (261, 264): "ntSH;s55" ). What is the best way to do this?
As for " The keys must be ordered according to the outer and the inner key lists " - keep in mind that dictionaries are unordered data structures.
OrderedDict
object is an alternative.
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)
The output:
{'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), '-')])}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.