从冻结集/冻结集的替代品中检索元素



我冻结了如下所示的输出:

以下数据只是一个示例。总的来说,我希望数据采用以下格式:

对于双打:

项目项置信度

对于三元组:

项目项目

项目置信度

双打:

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

如果没有使用冷冻套装的任何替代方案会有所帮助。

感谢您的阅读

对于您的"双打",您在其中设置了单个值。您可以通过几种不同的方式检索第一个(也是唯一的(值:

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

要设置双精度值的格式,您可以使用格式字符串:

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

相关内容

  • 没有找到相关文章

最新更新