将元素从 Frozenset/Alternatives 检索到 Frozenset

Retrieving elements from Frozenset/Alternatives to Frozenset

我的 frozenset 输出如下所示:

以下数据仅为示例。总的来说,我希望数据采用这种格式:

双打:

项目项目置信度

对于三胞胎:

项目项目项目信心

双打:

[(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)]

三胞胎:

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

我只是想知道是否可以检索元素,使输出采用以下格式:

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

如果没有,使用 frozenset 的任何替代方法都会有所帮助。

感谢阅读

对于您的 "doubles",您的集合中有单个值。您可以通过几种不同的方式检索第一个(也是唯一一个)值:

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

元组拆包:

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

转换为列表然后获取第一个索引:

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

创建迭代器,然后取next值:

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

使用生成器表达式 next:

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

您有一个表示双打的元组列表:

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

使用我向您展示的第一个元组解包方法,您可以解包这些值,全部在一个表达式中:

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

要格式化双精度值,您可以使用 format string:

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

一共:

>>> 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