123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import pickle
- def save_obj(obj, name):
- with open(name + '.pkl', 'wb') as f:
- pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
- def load_obj(name):
- with open(name + '.pkl', 'rb') as f:
- return pickle.load(f)
- def save_dict(dic, filename):
- with open(filename, 'wb') as f:
- pickle.dump(dic, f)
- def load_dict(filename):
- with open(filename, 'rb') as f:
- dic = pickle.load(f)
- return dic
- def iid_divide(l, g):
- """
- divide list l among g groups
- each group has either int(len(l)/g) or int(len(l)/g)+1 elements
- returns a list of groups
- """
- num_elems = len(l)
- group_size = int(len(l) / g)
- num_big_groups = num_elems - g * group_size
- num_small_groups = g - num_big_groups
- glist = []
- for i in range(num_small_groups):
- glist.append(l[group_size * i: group_size * (i + 1)])
- bi = group_size * num_small_groups
- group_size += 1
- for i in range(num_big_groups):
- glist.append(l[bi + group_size * i:bi + group_size * (i + 1)])
- return glist
|