Set#

class ivy.data_classes.container.set._ContainerWithSet(dict_in=None, queues=None, queue_load_sizes=None, container_combine_method='list_join', queue_timeout=None, print_limit=10, key_length_limit=None, print_indent=4, print_line_spacing=0, ivyh=None, default_key_color='green', keyword_color_dict=None, rebuild_child_containers=False, types_to_iteratively_nest=None, alphabetical_keys=True, dynamic_backend=None, build_callable=False, **kwargs)[source]#

Bases: ContainerBase

_abc_impl = <_abc._abc_data object>#
static _static_unique_all(x, /, *, axis=None, by_value=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container static method variant of ivy.unique_all. This method simply wraps the function, and so the docstring for ivy.unique_all also applies to this method with minimal changes.

Parameters:
  • x (Union[Array, NativeArray, Container]) – input container.

  • axis (Optional[Union[int, Container]], default: None) – the axis to apply unique on. If None, the unique elements of the flattened x are returned.

  • by_value (Union[bool, Container], default: True) – If False, the unique elements will be sorted in the same order that they occur in ‘’x’’. Otherwise, they will be sorted by value.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – A container of namedtuples (values, indices, inverse_indices, counts). The details can be found in the docstring for ivy.unique_all.

Examples

>>> x = ivy.Container(a=ivy.array([0., 1., 3. , 2. , 1. , 0.]),
...                   b=ivy.array([1,2,1,3,4,1,3]))
>>> y = ivy.Container.static_unique_all(x)
>>> print(y)
{
    a: [
        values = ivy.array([0., 1., 2., 3.]),
        indices = ivy.array([0, 1, 3, 2]),
        inverse_indices = ivy.array([0, 1, 3, 2, 1, 0]),
        counts = ivy.array([2, 2, 1, 1])
    ],
    b: [
        values = ivy.array([1, 2, 3, 4]),
        indices = ivy.array([0, 1, 3, 4]),
        inverse_indices = ivy.array([0, 1, 0, 2, 3, 0, 2]),
        counts = ivy.array([3, 1, 2, 1])
    ]
}
static _static_unique_counts(x, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container static method variant of ivy.unique_counts. This method simply wraps the function, and so the docstring for ivy.unique_counts also applies to this method with minimal changes.

Parameters:
  • x (Container) – input container. If x has more than one dimension, the function must flatten x and return the unique elements of the flattened array.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – a namedtuple (values, counts) whose

  • first element must have the field name values and must be an

array containing the unique elements of x. The array must have the same data type as x. - second element must have the field name counts and must be an array containing the number of times each unique element occurs in x. The returned array must have same shape as values and must have the default array index data type.

Examples

>>> x = ivy.Container(a=ivy.array([0., 1., 3. , 2. , 1. , 0.]),
...                   b=ivy.array([1,2,1,3,4,1,3]))
>>> y = ivy.Container.static_unique_counts(x)
>>> print(y)
{
    a:[values=ivy.array([0.,1.,2.,3.]),counts=ivy.array([2,2,1,1])],
    b:[values=ivy.array([1,2,3,4]),counts=ivy.array([3,1,2,1])]
}
static _static_unique_inverse(x, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container static method variant of ivy.unique_inverse. This method simply wraps the function, and so the docstring for ivy.unique_inverse also applies to this method with minimal changes.

Parameters:
  • x (Union[Array, NativeArray, Container]) – input container. If x has more than one dimension, the function must flatten x and return the unique elements of the flattened array.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – a namedtuple (values, inverse_indices) whose

  • first element must have the field name values and must be an array

containing the unique elements of x. The array must have the same data type as x. - second element must have the field name inverse_indices and

must be an array containing the indices of values that reconstruct x. The array must have the same shape as x and must have the default array index data type.

Examples

>>> x = ivy.Container(a=ivy.array([4.,8.,3.,5.,9.,4.]),
...                   b=ivy.array([7,6,4,5,6,3,2]))
>>> y = ivy.Container.static_unique_inverse(x)
>>> print(y)
{
    a:[values=ivy.array([3.,4.,5.,8.,9.]),inverse_indices=ivy.array([1,3,0,2,4,1])],
    b:[values=ivy.array([2,3,4,5,6,7]),inverse_indices=ivy.array([5,4,2,3,4,1,0])]
}
static _static_unique_values(x, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
Return type:

Container

unique_all(*, axis=None, by_value=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container instance method variant of ivy.unique_all. This method simply wraps the function, and so the docstring for ivy.unique_all also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container.

  • axis (Optional[Union[int, Container]], default: None) – the axis to apply unique on. If None, the unique elements of the flattened x are returned.

  • by_value (Union[bool, Container], default: True) – If False, the unique elements will be sorted in the same order that they occur in ‘’x’’. Otherwise, they will be sorted by value.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – A container of namedtuples (values, indices, inverse_indices, counts). The details of each entry can be found in the docstring for ivy.unique_all.

Examples

>>> x = ivy.Container(a=ivy.array([0., 1., 3. , 2. , 1. , 0.]),
...                   b=ivy.array([1,2,1,3,4,1,3]))
>>> y = x.unique_all()
>>> print(y)
[{
    a: ivy.array([0., 1., 2., 3.]),
    b: ivy.array([1, 2, 3, 4])
}, {
    a: ivy.array([0, 1, 3, 2]),
    b: ivy.array([0, 1, 3, 4])
}, {
    a: ivy.array([0, 1, 3, 2, 1, 0]),
    b: ivy.array([0, 1, 0, 2, 3, 0, 2])
}, {
    a: ivy.array([2, 2, 1, 1]),
    b: ivy.array([3, 1, 2, 1])
}]
unique_counts(*, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container instance method variant of ivy.unique_counts. This method simply wraps the function, and so the docstring for ivy.unique_counts also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container. If x has more than one dimension, the function must flatten x and return the unique elements of the flattened array.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – a namedtuple (values, counts) whose

  • first element must have the field name values and must be an

array containing the unique elements of x. The array must have the same data type as x. - second element must have the field name counts and must be an array containing the number of times each unique element occurs in x. The returned array must have same shape as values and must have the default array index data type.

Examples

With ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([0., 1., 3. , 2. , 1. , 0.]),
...                   b=ivy.array([1,2,1,3,4,1,3]))
>>> y = x.unique_counts()
>>> print(y)
[{
    a: ivy.array([0., 1., 2., 3.]),
    b: ivy.array([1, 2, 3, 4])
}, {
    a: ivy.array([2, 2, 1, 1]),
    b: ivy.array([3, 1, 2, 1])
}]
unique_inverse(*, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

ivy.Container instance method variant of ivy.unique_inverse. This method simply wraps the function, and so the docstring for ivy.unique_inverse also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container. If x has more than one dimension, the function must flatten x and return the unique elements of the flattened array.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

Return type:

Container

Returns:

ret – a namedtuple (values, inverse_indices) whose

  • first element must have the field name values and must be an array

containing the unique elements of x. The array must have the same data type as x. - second element must have the field name inverse_indices and

must be an array containing the indices of values that reconstruct x. The array must have the same shape as x and must have the default array index data type.

Examples

>>> x = ivy.Container(a=ivy.array([4.,8.,3.,5.,9.,4.]),
...                   b=ivy.array([7,6,4,5,6,3,2]))
>>> y = x.unique_inverse()
>>> print(y)
[{
    a: ivy.array([3., 4., 5., 8., 9.]),
    b: ivy.array([2, 3, 4, 5, 6, 7])
}, {
    a: ivy.array([1, 3, 0, 2, 4, 1]),
    b: ivy.array([5, 4, 2, 3, 4, 1, 0])
}]
>>> x = ivy.Container(a=ivy.array([1., 4., 3. , 5. , 3. , 7.]),
...                   b=ivy.array([3, 2, 6, 3, 7, 4, 9]))
>>> y = ivy.ivy.unique_inverse(x)
>>> print(y)
[{
    a: ivy.array([1., 3., 4., 5., 7.]),
    b: ivy.array([2, 3, 4, 6, 7, 9])
}, {
    a: ivy.array([0, 2, 1, 3, 1, 4]),
    b: ivy.array([1, 0, 3, 1, 4, 2, 5])
}]
unique_values(*, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.unique_values. This method simply wraps the function and applies it on the container.

Parameters:
  • self (ivy.Container) – input container

  • key_chains (list or dict, optional) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (bool, optional) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (bool, optional) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (bool, optional) – Whether to also map method to sequences (lists, tuples). Default is False.

  • out (ivy.Container, optional) – The container to return the results in. Default is None.

Return type:

Container

Returns:

ivy.Container – The result container with the unique values for each input key-chain.

Raises:
  • TypeError – If the input container is not an instance of ivy.Container.

  • ValueError – If the key_chains parameter is not None, and it is not a list or a dictionary.

Example

>>> x = ivy.Container(a=[1, 2, 3], b=[2, 2, 3], c=[4, 4, 4])
>>> y = x.unique_values()
>>> print(y)
{
    a: ivy.array([1, 2, 3]),
    b: ivy.array([2, 3]),
    c: ivy.array([4])
}
>>> x = ivy.Container(a=[1, 2, 3], b=[2, 2, 3], c=[4, 4, 4])
>>> y = x.unique_values(key_chains=["a", "b"])
>>> print(y)
{
    a: ivy.array([1, 2, 3]),
    b: ivy.array([2, 3]),
    c: [
        4,
        4,
        4
    ]
}

This should have hopefully given you an overview of the set submodule, if you have any questions, please feel free to reach out on our discord!