def ch_payload(payload):
result = {}
for key, value in payload.items():
if isinstance(value, list):
if value:
value = value[0]
result[key] = value
if isinstance(value, dict):
result[key] = ch_payload(value)
return result
def get_value(payload, item, fallback=None):
"""
Получение вложенных данных
d = {'a': {'b': {'c': 1}}}
get_value(d, 'a.b.c')
"""
serialize_dict = ch_payload(payload)
def getitem(serialize_dict, name):
try:
return serialize_dict[name]
except (KeyError, TypeError):
return fallback
return reduce(getitem, item.split('.'), serialize_dict)
test = get_value(newMessage, "messages.items.peer_id")
start = [
[0, 4, 0, 1, 0],
[0, 0, 2, 3, 0],
[0, 0, 3, 1, 0],
[0, 0, 0, 0, 0],
]
result = [list(t) for t in zip(*[i for i in zip(*[x for x in start if any(x)]) if any(i)])]