aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aly@aaronly.me>2019-02-28 21:36:37 +0800
committerAaron LI <aly@aaronly.me>2019-02-28 21:36:37 +0800
commit4721943aecb8b7c7f2cf3cb726a41b5edd3e8d49 (patch)
tree77947eaf5fd1d8b9bac73aca223a9f4035c041d1
parentf5c44a03dfeffd8d0c153258292d91ee0741aa7d (diff)
downloadfg21sim-4721943aecb8b7c7f2cf3cb726a41b5edd3e8d49.tar.bz2
utils/ds: Add pad_dict_list() function
-rw-r--r--fg21sim/utils/ds.py30
1 files changed, 26 insertions, 4 deletions
diff --git a/fg21sim/utils/ds.py b/fg21sim/utils/ds.py
index 8b69dbc..694cbe3 100644
--- a/fg21sim/utils/ds.py
+++ b/fg21sim/utils/ds.py
@@ -1,5 +1,5 @@
-# Copyright (c) 2017 Weitian LI <weitian@aaronly.me>
-# MIT license
+# Copyright (c) 2017,2019 Weitian LI <wt@liwt.net>
+# MIT License
"""
Data structure/set utilities.
@@ -78,5 +78,27 @@ def dictlist_to_dataframe(dictlist, keys=None):
dv = [d[k] for k in keys]
dv2 = list(_flatten_list(dv))
data.append(dv2)
- dataframe = pd.DataFrame(data, columns=columns)
- return dataframe
+
+ return pd.DataFrame(data, columns=columns)
+
+
+def pad_dict_list(d, keys, length, fill=None):
+ """
+ Pad the lists specified by ``keys`` to the given ``length``.
+
+ Parameters
+ ----------
+ d : dict
+ The dictionary to be updated.
+ keys : list[str]
+ The keys of the lists in the dictionary to be padded.
+ length : int
+ The expected length.
+ fill : optional
+ The value filled to the padded list.
+ """
+ for k in keys:
+ v = d[k]
+ n = len(v)
+ if n < length:
+ d[k] = v + [fill] * (length - n)