Data Structure Skill
Dictionary :
Get the key of max/min val :
Type checking
Simple way:
Or use pandas helper:
Danger! List-like can include some unexpected type. Avoid to use pandas helper
Set
Merge list into a set
Dict of List
Declare a dict of list for adding value easily
from collections import defaultdict
from typing import Dict, List
# so, element is init with a list
dict_list: Dict[str, List[str]] = defaultdict(list)
# Then you can do like this without error
dict_list["not_existing_key"].append(1000)
# {'not_existing_key': [1000]}
Convert Dict of List → List of Dict (not same length)
import numpy as np
import itertools
DL = {
"10_ele": np.ones(10),
"5_ele": np.ones(5),
"2_ele": np.ones(2),
}
# {'10_ele': array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]),
# '5_ele': array([1., 1., 1., 1., 1.]),
# '2_ele': array([1., 1.])}
v = [dict(zip(DL,t)) for t in itertools.zip_longest(*DL.values(), fillvalue="")]
"""output
[{'10_ele': 1.0, '5_ele': 1.0, '2_ele': 1.0},
{'10_ele': 1.0, '5_ele': 1.0, '2_ele': 1.0},
{'10_ele': 1.0, '5_ele': 1.0, '2_ele': ''},
{'10_ele': 1.0, '5_ele': 1.0, '2_ele': ''},
{'10_ele': 1.0, '5_ele': 1.0, '2_ele': ''},
{'10_ele': 1.0, '5_ele': '', '2_ele': ''},
{'10_ele': 1.0, '5_ele': '', '2_ele': ''},
{'10_ele': 1.0, '5_ele': '', '2_ele': ''},
{'10_ele': 1.0, '5_ele': '', '2_ele': ''},
{'10_ele': 1.0, '5_ele': '', '2_ele': ''}]
"""
Find the largest len of nested list