从Frozenset / Alternatives中检索元素到Frozenset

时间:2018-01-28 21:16:56

标签: python algorithm set apriori 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

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

感谢您阅读

1 个答案:

答案 0 :(得分:3)

为你的"双打"你有一些单值的集合。您可以通过几种不同的方式检索第一个(也是唯一的)值:

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