1

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

Srikar Murali
  • 135
  • 2
  • 15
  • The brackets in the data are not matching. Can you please first fix input and output? – Willem Van Onsem Jan 28 '18 at 21:18
  • The first numerical values in the 2 lists have `1.0` and `0.8`. What happens to them? – Peter Wood Jan 28 '18 at 21:20
  • `print '\n'.join('{} {} {}'.format(first.pop(), second.pop(), third for first, second, third in as))` – Peter Wood Jan 28 '18 at 21:23
  • My apologies for the poor output I will fix them. To the other question, the output is just an example of how it should look when taken from a larger dataset. The output given is just a sample that I want to format into the output shown above. – Srikar Murali Jan 28 '18 at 22:18
  • My apologies but I don't really understand that solution. Wasn't able to get it to work either. Why are the variables part of "as"? – Srikar Murali Jan 28 '18 at 22:22
  • `as` was meant to be a variable name for your list of `a` values for `Output A` (what you've now named `doubles` – Peter Wood Jan 28 '18 at 23:32
  • @SrikarMurali Sorry, the `pop` wasn't correct for `frozenset`. Have posted answer. – Peter Wood Jan 29 '18 at 00:00

1 Answers1

5

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
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • Brilliant Thank you! Just wondering but would this be the code for the triples: triple_format = '{} {} {} {:0.4f}' for triple in triples: (first,), (second,), (third,), fourth = triple print(triple_format.format(first, second, third, fourth)) Cause I tried this but its not working for some reason. – Srikar Murali Jan 29 '18 at 00:32
  • 1
    Actually never mind I figured it out! I had to do (first,), (second1,second2), third for the unpacking since the second tuple has two values! Thank you for the detailed explanation – Srikar Murali Jan 29 '18 at 00:35