Search code examples
pythonalgorithmsetapriorifrozenset

Retrieving elements from Frozenset/Alternatives to Frozenset


I have frozenset output that looks like this:

The data below is just an example. Overall I want the data to be in this format:

For doubles:

Item Item Confidence

For Triples:

Item Item Item Confidence

Doubles:

[(frozenset({'GRO73461'}), frozenset({'ELE17451'}), 1.0), (frozenset({'ELE26917'}), frozenset({'GRO99222'}), 1.0), 
 (frozenset({'SNA80192'}), frozenset({'ELE17451'}), 1.0), (frozenset({'DAI22896'}), frozenset({'ELE17451'}), 0.9), 
 (frozenset({'GRO99222'}), frozenset({'ELE17451'}), 0.8125)]

Triples:

[(frozenset({'DAI22896'}), frozenset({'GRO73461', 'ELE17451'}), 0.8), (frozenset({'GRO73461'}), 
  frozenset({'ELE17451', 'DAI22896'}), 0.8), (frozenset({'ELE17451'}), frozenset({'GRO73461', 'DAI22896'}), 0.3076923076923077)]

I was just wondering if it was possible to retrieve the element such that the output is in this format:

OUTPUT A
FRO11987 FRO12685 0.4325
FRO11987 ELE11375 0.4225
FRO11987 GRO94758 0.4125
FRO11987 SNA80192 0.4025
FRO11987 FRO18919 0.4015
OUTPUT B
FRO11987 FRO12685 DAI95741 0.4325
FRO11987 ELE11375 GRO73461 0.4225
FRO11987 GRO94758 ELE26917 0.4125
FRO11987 SNA80192 ELE28189 0.4025
FRO11987 FRO18919 GRO68850 0.4015

If not any alternatives to using a frozenset would be helpful.

Thank you for reading


Solution

  • For your "doubles" you have sets with single values in the them. You can retrieve the first (and only) value a few different ways:

    >>> s = frozenset({'GRO73461'})
    

    Tuple unpacking:

    >>> value, = s
    >>> value
    'GRO73461'
    

    Convert to a list then take the first index:

    >>> list(s)[0]
    'GRO73461'
    

    Create an iterator, then take the next value:

    >>> next(iter(s))
    'GRO73461'
    

    Use a generator expression with next:

    >>> next(value for value in s)
    'GRO73461'
    

    You have a list of tuples representing doubles:

    >>> double = (frozenset({'GRO73461'}), frozenset({'ELE17451'}), 1.0)
    

    Using the first tuple unpacking method I showed you, you can unpack these values, all in one expression:

    >>> (first,), (second,), third = double
    >>> first, second, third
    'GRO73461', 'ELE17451', 1.0
    

    To format the double values you can use a format string:

    >>> double_format = '{} {} {:0.4f}'
    >>> double_format.format(first, second, third)
    'GRO73461 ELE17451 1.0000'
    

    Altogether:

    >>> doubles = [
    ...     (frozenset({'GRO73461'}), frozenset({'ELE17451'}), 1.0), 
    ...     (frozenset({'ELE26917'}), frozenset({'GRO99222'}), 1.0),
    ...     (frozenset({'SNA80192'}), frozenset({'ELE17451'}), 1.0), 
    ...     (frozenset({'DAI22896'}), frozenset({'ELE17451'}), 0.9),
    ...     (frozenset({'GRO99222'}), frozenset({'ELE17451'}), 0.8125)
    ... ]
    >>> for double in doubles:
    ...     (first,), (second,), third = double
    ...     print double_format.format(first, second, third)
    GRO73461 ELE17451 1.0000
    ELE26917 GRO99222 1.0000
    SNA80192 ELE17451 1.0000
    DAI22896 ELE17451 0.9000
    GRO99222 ELE17451 0.8125