-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_utils_list.py
69 lines (52 loc) · 2.61 KB
/
lib_utils_list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Library Features:
Name: lib_utils_list
Author(s): Francesco Avanzi (francesco.avanzi@cimafoundation.org), Fabio Delogu (fabio.delogu@cimafoundation.org)
Date: '20210607'
Version: '3.0.0'
"""
#######################################################################################
# Library
import logging
from lib_default_args import logger_name
# Log
log_stream = logging.getLogger(logger_name)
# Debug
# import matplotlib.pylab as plt
#######################################################################################
# -------------------------------------------------------------------------------------
# Method to pad or truncate list
def pad_or_truncate_list(some_list, target_len, fill_value=-9999.0):
return some_list[:target_len] + [fill_value]*(target_len - len(some_list))
# -------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------
# Method to flat list of list
def flat_list(lists):
return [item for sublist in lists for item in sublist]
# -------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------
# Method to convert coupled list to dictionary
def merge_lists_4_dict(list_keys, list_values):
dictionary = {k: v for k, v in zip(list_keys, list_values)}
return dictionary
# -------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------
# Method to convert list 2 dictionary
def convert_list_2_dict(var_list):
var_dict = {}
for step, var in enumerate(var_list):
var_dict[step] = var
return var_dict
# -------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------
# Method to convert list to string (given a delimiter)
def convert_list_2_string(list_data, list_delimiter=','):
string_data = list_delimiter.join(str(elem) for elem in list_data)
return string_data
# -------------------------------------------------------------------------------------
# -------------------------------------------------------------------------------------
# Method to convert string to list (given a delimiter)
def convert_string_2_list(str_data, str_delimiter=' '):
list_data = str_data.split(str_delimiter)
return list_data
# -------------------------------------------------------------------------------------