gpt4 book ai didi

python - 从 Frozenset/Alternatives 到 Frozenset 检索元素

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:41:50 24 4
gpt4 key购买 nike

我的 frozenset 输出看起来像这样:

下面的数据只是一个例子。总的来说,我希望数据采用这种格式:

对于 double :

项目项目置信度

对于三元组:

项目项目项目置信度

double :

[(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 的任何替代方法都会有所帮助。

感谢阅读

最佳答案

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

>>> 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 的元组列表:

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

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

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

要格式化 double 值,您可以使用 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

关于python - 从 Frozenset/Alternatives 到 Frozenset 检索元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48491412/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com