How do you divide a dictionary in python?

29 Python code examples are found related to " split dict". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

Example 1

def splitDict(Data):
    """
    Split a dictionary with lists as the data, into smaller dictionaries

    :param Data: A dictionary with lists as the values

    :return: A tuple of dictionaries each containing the data separately,
            with the same dictionary keys
    """
    # find the maximum number of items in the dictionary
    maxitems = max([len(values) for values in Data.values()])
    output =[dict() for i in range(maxitems)]
    for key, values in Data.items():
        for i, val in enumerate(values):
            output[i][key] = val

    return tuple(output) 

Example 2

def split_array_to_dict(data, param_names):
    """Create a dictionary out of an array.

    This basically splits the given nd-matrix into sub matrices based on the second dimension. The length of
    the parameter names should match the length of the second dimension. If a two dimensional matrix of shape (d, p) is
    given we return p matrices of shape (d,). If a matrix of shape (d, p, s_1, s_2, ..., s_n) is given, we return
    p matrices of shape (d, s_1, s_2, ..., s_n).

    This is basically the inverse of :func:`combine_dict_to_array`.

    Args:
        data (ndarray): a multidimensional matrix we index based on the second dimension.
        param_names (list of str): the names of the parameters, one per column

    Returns:
        dict: the results packed in a dictionary
    """
    if data.shape[1] != len(param_names):
        raise ValueError('The number of columns ({}) in the matrix does not match '
                         'the number of dictionary keys provided ({}).'.format(data.shape[1], len(param_names)))
    return {name: data[:, i, ...] for i, name in enumerate(param_names)} 

Example 3

def splitDict(Data):
    """
    Split a dictionary with lists as the data, into smaller dictionaries

    :param Data: A dictionary with lists as the values

    :return: A tuple of dictionaries each containing the data separately,
            with the same dictionary keys
    """
    # find the maximum number of items in the dictionary
    maxitems = max([len(values) for values in Data.values()])
    output =[dict() for i in range(maxitems)]
    for key, values in Data.items():
        for i, val in enumerate(values):
            output[i][key] = val

    return tuple(output) 

Example 4

def split_json_dict(args):
    num, infile, out1, out2 = parse_args(args)
    data = {}
    with open(infile) as f:
        data = json.load(f, object_pairs_hook=collections.OrderedDict)
    if not data:
        fail("Baremetal data file %s is empty or non-valid JSON." % infile)

    first_data = collections.OrderedDict()
    second_data = collections.OrderedDict()
    for i, k in enumerate(data):
        if i < num:
            first_data[k] = data[k]
        else:
            second_data[k] = data[k]

    write_to_json(out1, first_data)
    write_to_json(out2, second_data) 

Example 5

def split_dict_to_subdicts(dct: Dict, prefixes: List, extra_key: str):
    """@TODO: Docs. Contribution is welcome."""
    subdicts = {}
    extra_subdict = {
        k: v
        for k, v in dct.items()
        if all(not k.startswith(prefix) for prefix in prefixes)
    }
    if len(extra_subdict) > 0:
        subdicts[extra_key] = extra_subdict
    for prefix in prefixes:
        subdicts[prefix] = {
            k.replace(f"{prefix}_", ""): v
            for k, v in dct.items()
            if k.startswith(prefix)
        }
    return subdicts 

Example 6

def split_dict_column_value_pair(pk,row):
    """
     产生基于主键的dict类型的{列名,列值}
    :param pk: 可能是string或者tuple
    :param row:
    :return: 其他列字典{列名,列值} ,主键字典{列名,列值}
    """
    pk_orgi={}
    pk_dt={}
    other_dt={}
    if isinstance(pk,unicode):
        pk_orgi[pk]=""
    else:
        for key in pk:
            pk_orgi[key]=""
    for key in row:
       if pk_orgi.has_key(key):
           pk_dt[key]=row[key]
       else:
           other_dt[key]=row[key]
    return (other_dt,pk_dt) 

Example 7

def split_sequence_dict(data_dict, fraction=.5):
    """Splits sequences into sequences of length `fraction` and 1 - `fraction`

        `data_dict` is a dict of k: v pairs, where vs are lists of sequences.
        Every sequence in the list is split in two and put into two separate dicts.

        If any of the sequences in a pair has length of 1, then it is concatenated to the
         other one and put only in one of the dicts

    :param data_dict:
    :param fraction:
    :return: tuple of dicts with split sequences
    """
    a, b = dict(), dict()

    for k, v in data_dict.iteritems():
        a[k], b[k] = [], []
        for i in xrange(len(v)):
            sa, sb = split_sequence(v[i], fraction)
            if sa is not None:
                a[k].append(sa)
            if sb is not None:
                b[k].append(sb)

    return a, b 

Example 8

def get_split_names_for_scg_taxonomy_super_dict(self, scg_taxonomy_super_dict):
        """Returns a list of split names associated with SCGs found in a scg_taxonomy_super_dict."""

        if 'scgs' not in list(scg_taxonomy_super_dict['taxonomy'].values())[0]:
            raise ConfigError("Someone called this function with something that doesn't look like the kind "
                              "of input data it was expecting (sorry for the vagueness of the message, but "
                              "anvi'o hopes that will be able to find out why it is happening).")

        split_names = set([])

        for entry_name in scg_taxonomy_super_dict['taxonomy']:
            for gene_callers_id in scg_taxonomy_super_dict['taxonomy'][entry_name]['scgs']:
                split_names.add(self.gene_callers_id_to_split_name_dict[gene_callers_id])

        return split_names 

Example 9

def get_split_xml_dict(self, split):
        if split == 'train':
            xml_dict = self.train_xml_dict
            split_annotation_path = self.train_annotation_path
        elif split == 'val':
            xml_dict = self.val_xml_dict
            split_annotation_path = self.val_annotation_path
        for i, synset in enumerate(self.train_annotation_synsets):
            print i
            synset_xml_files = glob.glob1(os.path.join(split_annotation_path, synset), '*.xml')
            xml_dict[synset] = synset_xml_files
        print split, 'annotation run-through complete.'
        print len(xml_dict.keys() ) 

Example 10

def get_split_im_dict(self, split):
        if split == 'train':
            split_path = self.train_set_path
            split_dict = self.train_im_dict
        elif split == 'val':
            split_path = self.val_set_path
            split_dict = self.val_im_dict
        for i, synset in enumerate(self.train_synsets): # val and train have same synsets!
            print i
            synset_im_files = glob.glob1(os.path.join(split_path, synset), '*.JPEG')
            split_dict[synset] = synset_im_files
        print split, ' set run-through complete.'
        print len(split_dict.keys() ) 

Example 11

def split_paths_to_dict(paths):
    rewards, terminals, obs, actions, next_obs = split_paths(paths)
    return dict(
        rewards=rewards,
        terminals=terminals,
        observations=obs,
        actions=actions,
        next_observations=next_obs,
    ) 

Example 12

def split_paths_to_dict(paths):
    rewards, terminals, obs, actions, next_obs = split_paths(paths)
    return dict(
        rewards=rewards,
        terminals=terminals,
        observations=obs,
        actions=actions,
        next_observations=next_obs,
    ) 

Example 13

def split_tensor_dict_list(tensor_dict):
    keys = list(tensor_dict.keys())
    ret = None
    for k in keys:
        vals = tensor_dict[k]
        if isinstance(vals, dict):
            vals = split_tensor_dict_list(vals)
        if ret is None:
            ret = [{k: v} for v in vals]
        else:
            for v, cur_dict in zip(vals, ret):
                cur_dict[k] = v
    return ret 

Example 14

def split_tensor_dict_list(tensor_dict):
    """Split a list of dictionaries of {tensors or dictionary of tensors}.

    Args:
        tensor_dict (dict): a list of dictionaries of {tensors or
        dictionary of tensors}.

    Returns:
        dict: a dictionary of {split tensors or dictionary of split tensors}.
    """
    keys = list(tensor_dict.keys())
    ret = None
    for k in keys:
        vals = tensor_dict[k]
        if isinstance(vals, dict):
            vals = split_tensor_dict_list(vals)
        if ret is None:
            ret = [{k: v} for v in vals]
        else:
            for v, cur_dict in zip(vals, ret):
                cur_dict[k] = v
    return ret 

Example 15

def split_tensor_dict_list(tensor_dict):
    keys = list(tensor_dict.keys())
    ret = None
    for k in keys:
        vals = tensor_dict[k]
        if isinstance(vals, dict):
            vals = split_tensor_dict_list(vals)
        if ret is None:
            ret = [{k: v} for v in vals]
        else:
            for v, cur_dict in zip(vals, ret):
                cur_dict[k] = v
    return ret 

Example 16

def split_dict_evenly(m_dict, segment_count):
    if segment_count == 1:
        return [m_dict]

    segment_length = math.ceil(len(m_dict) / segment_count)
    keys = list(m_dict.keys())
    key_groups = [keys[segment_length * i: segment_length * (i + 1)] for i in range(segment_count)]
    return [{key: m_dict[key] for key in group} for group in key_groups]


# a single downloader thread 

Example 17

def split_dict_to_list_of_dict(
        dict_with_tensors: Dict[str, tf.Tensor],
        num_of_splits: int,
        axis=0,
        pad_to_batch=False) -> List[Dict[str, tf.Tensor]]:
    """
    Split the dict of tensors to list of dicts with same structure

    Parameters
    ----------
    dict_with_tensors
        dict with tensors
    num_of_splits
        number of splits
    axis
        axis to split the tensors
    pad_to_batch
        whether to pad

    Returns
    -------
    list_of_dicts
        list of split dicts

    """
    if pad_to_batch:
        split_fn = tf_ops.split_with_pad
    else:
        split_fn = tf.split
    dict_split = {k: split_fn(inp, num_of_splits, axis)
                  for k, inp in dict_with_tensors.items()}
    dict_list_split = []
    for i in range(num_of_splits):
        subdict = {k: inp_spl[i] for k, inp_spl in dict_split.items()}
        dict_list_split.append(subdict)
    return dict_list_split 

Example 18

def SplitDictOfTensors(t_dict, num_splits):
  """Splits tensors in `t_dict` evenly into `num_splits` along the 1st dimenion.

  Args:
    t_dict: A dictionary of tensors. Each tensor's 1st dimension is the same
      size.
    num_splits: A python integer.

  Returns:
    A list of dictionaries of tensors, num elements in the list = num_splits

    i-th dictionary in the list corresponds to i-th split of each tensor
    along the first dimension of each tensor for each key in the original dict.
  """
  keys = []
  values = []
  for k, v in sorted(six.iteritems(t_dict)):
    keys.append(k)
    values.append(v)

  splits = SplitTensors(tuple(values), num_splits)

  assert all(len(lst) == len(splits[0]) for lst in splits)

  ret_list = []
  for s in range(num_splits):
    d = {}
    for k in range(len(splits)):
      d[keys[k]] = splits[k][s]
    ret_list.append(d)

  return ret_list 

Example 19

def split_paths_to_dict(paths):
    rewards, terminals, obs, actions, next_obs = split_paths(paths)
    return dict(
        rewards=rewards,
        terminals=terminals,
        observations=obs,
        actions=actions,
        next_observations=next_obs,
    ) 

Example 20

def split_dict(d, first_predicate):
    """split the dictionary d into 2 dictionaries, first one contains elements validating first_predicate"""
    first, second = OrderedDict(), OrderedDict()
    for key, value in d.items():
        if first_predicate(key):
            first[key] = value
        else:
            second[key] = value
    return first, second 

Example 21

def build_split_dict(X: pd.DataFrame, split_obj: Type[BaseCrossValidator]) -> dict:
        """
        Get dictionary of cross-validation training dataset split metadata

        Parameters
        ----------
        X: pd.DataFrame
            The training dataset that will be split during cross-validation.
        split_obj: Type[sklearn.model_selection.BaseCrossValidator]
            The cross-validation object that returns train, test indices for splitting.

        Returns
        -------
        split_metadata: Dict[str,Any]
            Dictionary of cross-validation train/test split metadata
        """
        split_metadata: Dict[str, Any] = dict()
        for i, (train_ind, test_ind) in enumerate(split_obj.split(X)):
            split_metadata.update(
                {
                    f"fold-{i+1}-train-start": X.index[train_ind[0]],
                    f"fold-{i+1}-train-end": X.index[train_ind[-1]],
                    f"fold-{i+1}-test-start": X.index[test_ind[0]],
                    f"fold-{i+1}-test-end": X.index[test_ind[-1]],
                }
            )
            split_metadata.update({f"fold-{i+1}-n-train": len(train_ind)})
            split_metadata.update({f"fold-{i+1}-n-test": len(test_ind)})
        return split_metadata 

Example 22

def split_tensor_dict_list(tensor_dict):
    keys = list(tensor_dict.keys())
    ret = None
    for k in keys:
        vals = tensor_dict[k]
        if isinstance(vals, dict):
            vals = split_tensor_dict_list(vals)
        if ret is None:
            ret = [{k: v} for v in vals]
        else:
            for v, cur_dict in zip(vals, ret):
                cur_dict[k] = v
    return ret 

Example 23

def split_dict_py_tf(dictionary):
  """Splits dictionary based on Python and TensorFlow values.

  Args:
    dictionary: An arbitrary `dict`. Any `dict` objects in values will be
      processed recursively.

  Returns:
    A tuple `(d_py, d_tf)`, where
    d_py: A `dict` of the same structure as `dictionary`, with TensorFlow values
      removed, recursively.
    d_tf: A `dict` of the same structure as `dictionary`, with non-TensorFlow
      values removed, recursively.

  Raises:
    TypeError:
      If the input is not a `dict` object.
  """
  if not isinstance(dictionary, dict):
    raise TypeError
  d_py, d_tf = {}, {}
  for k, v in six.iteritems(dictionary):
    if isinstance(v, dict):
      d_py[k], d_tf[k] = split_dict_py_tf(v)
    else:
      if tf.is_tensor(v):
        d_tf[k] = v
      else:
        d_py[k] = v
  return d_py, d_tf 

Example 24

def replace_dict_keys_split(dicts, replace_list_dict):
    """ Replace values in `dicts` according to :attr:`replace_list_dict`.

    Replaced dict is splitted by :attr:`replaced_dict` and :attr:`remain_dict`.

    Parameters
    ----------
    dicts : dict
        Dictionary.
    replace_list_dict : dict
        Dictionary.

    Returns
    -------
    replaced_dict : dict
        Dictionary.
    remain_dict : dict
        Dictionary.

    Examples
    --------
    >>> replace_list_dict = {'a': 'loc'}
    >>> x_dict = {'a': 0, 'b': 1}
    >>> print(replace_dict_keys_split(x_dict, replace_list_dict))
    ({'loc': 0}, {'b': 1})

    """
    replaced_dict = {replace_list_dict[key]: value for key, value in dicts.items()
                     if key in list(replace_list_dict.keys())}

    remain_dict = {key: value for key, value in dicts.items()
                   if key not in list(replace_list_dict.keys())}

    return replaced_dict, remain_dict


# immutable dict class 

Example 25

def split_tensor_dict_list(tensor_dict):
    keys = list(tensor_dict.keys())
    ret = None
    for k in keys:
        vals = tensor_dict[k]
        if isinstance(vals, dict):
            vals = split_tensor_dict_list(vals)
        if ret is None:
            ret = [{k: v} for v in vals]
        else:
            for v, cur_dict in zip(vals, ret):
                cur_dict[k] = v
    return ret 

Example 26

def split_dict(dic, condition):
    dict_1 = {}
    dict_2 = {}
    for key, val in dic.items():
        if condition(key, val):
            dict_1.update({key: val})
        else:
            dict_2.update({key: val})
    return dict_1, dict_2 

Example 27

def split_string_to_dict(string, seperator):
    tokens = string.split(seperator)
    if len(tokens) >= 2:
        return {tokens[0]: tokens[1]}

    return None 

Example 28

def split_to_dict(string):
        splitter = shlex.shlex(string, posix=True)
        splitter.whitespace_split = True
        splitter.whitespace = ","
        tags_dict = dict(pair.split("=", 1) for pair in splitter)
        return tags_dict 

Example 29

def split_dict(inp, *keyses):
    """Split an input dict into multiple dicts according to lists of keys

    Args:
        ind (dict): input dictionary, usually kwargs
        *keyses (tuple of lists): Variable length argument list that
            contains keys to split.  That is, keyses itself is a tuple
            of lists of keys, and each of keyses[0], keyses[1], ... is
            a list of keys.

    Returns:
        dict or tuple of dicts: If no keyses is provided, return a
            dictionary, which should contain the same key-value pairs
            of `inp`.  Otherwise, return a tuple of dicts that can be
            unwrapped into a form similar to the full argument list.

    Example:
        To illustrate how split_dict() can be used, we provide two
        examples here.  The non-trivial one is:

            $ kwargs = {'a': 1, 'b': 2, 'c': 3, 'd': 4}
            $ keys1  = ['b']
            $ keys2  = ['b', 'c']
            $ d0, d1, d2 = split_dict(kwargs, keys1, keys2)

        `d0`, `d1`, and `d2` above should be `{'a': 1, 'd': 4}`,
        `{'b': 2}`, and `{'b': 2, 'c': 3}`, respectively.

        We desin split_dic() so it is intuitive to use.  A special
        case where no list of keys is passed, which is our second
        exampmle:

            $ d0 = split_dict(kwargs)

        `d0` should contain the same key-value pairs of kwargs.

    """
    out = tuple({} for i in range(len(keyses)+1))

    for k, v in inp.items():
        matched = False
        for i, keys in enumerate(keyses):
            if k in keys:
                matched = True
                out[i+1].update({k: v})
        if not matched:
            out[0].update({k: v})

    return out[0] if keyses == () else out 

Can you divide dictionaries in Python?

Python division operation on Dict Python division operation can be performed on the elements present in the dictionary using Counter() function along with '//' operator.

How do you split a dictionary by key in Python?

Use data. viewkeys() if you are using Python 2 still. Dictionary views give you a set-like object, on which you can then use set operations; & gives you the intersection.

What is the division key in Python?

In Python, there are two types of division operators: / : Divides the number on its left by the number on its right and returns a floating point value. // : Divides the number on its left by the number on its right, rounds down the answer, and returns a whole number.

How do you break nested dictionaries in Python?

In Python, we use “ del “ statement to delete elements from nested dictionary.