gpt4 book ai didi

python - 查找列表之间的差异并将差异附加到列表,但对于 40 个不同的列表 - python

转载 作者:太空宇宙 更新时间:2023-11-04 07:42:01 24 4
gpt4 key购买 nike

您好,很难在标题中正确解释这一点,但首先让我先解释一下我的数据。我有 40 个列表存储在一个列表中,其形式如下:

data[0] = [[value1 value2 value3,80],[value1,90],[value1 value3,60],[value2 value3,70]]
data[1] = [[value2,40],[value1 value2 value3,90]]
data[2] = [[value1 value2,80],[value1,50],[value1 value3,20]]
.
.
.

现在我期待这样的输出:

data[0] = [[value1 value2 value3,80],[value1,90],[value1 value3,60],[value2 value3,70],[value2,0],[value1 value2,0]]
data[1] = [[value2,40],[value1 value2 value3,90],[value1,0],[value1 value3,0],[value2 value3,0],[value1 value2,0]]
data[2] = [[value1 value2,80],[value1,50],[value1 value3,20],[value1 value2 value3,0],[value2 value3,0],[value2,0]]

我知道这读起来有点复杂,但我想确保有一个很好的数据演示。所以基本上所有列表都需要具有所有列表中存在的值的所有可能组合,如果该组合不作为标准出现在该列表中,那么它的频率(第二个字段)为 0。

感谢您的帮助,请记住这是 40 个不同列表的交集,因此需要快速高效。我不确定如何最好地做到这一点......

编辑:我也不知道所有的“值”,为了简单起见,我刚刚在这里写了 3 个不同的值(值 1、值 2、值 3)。在我的项目中,我不知道值是什么或者有多少不同的值(我知道至少有几千个)

编辑 2:这是一些真实的输入数据,我没有真实的输出数据,但我会尝试解决它:

data[0] = [['destination_ip:10.32.0.100 destination_service:http destination_port:80 protocol:TCP syslog_priority:Info', '39.7769'], ['destination_ip:10.32.0.100 destination_service:http destination_port:80 protocol:TCP', '39.7769'], ['destination_ip:10.32.0.100 destination_service:http destination_port:80 syslog_priority:Info', '39.7769'], ['destination_ip:10.32.0.100 destination_service:http destination_port:80', '39.7769'], ['destination_ip:10.32.0.100 destination_service:http protocol:TCP syslog_priority:Info', '39.7769']]


data[1] = [['syslog_priority:Info', '100'], ['destination_ip:10.32.0.100 syslog_priority:Info destination_service:http destination_port:80 protocol:TCP', '43.8362'], ['destination_ip:10.32.0.100 syslog_priority:Info destination_service:http destination_port:80', '43.8362'], ['destination_ip:10.32.0.100 syslog_priority:Info destination_service:http protocol:TCP', '43.8362'], ['destination_ip:10.32.0.100 syslog_priority:Info destination_service:http', '43.8362']]


data[2] = [['destination_ip:10.32.0.100 destination_port:80 destination_service:http syslog_priority:Info protocol:TCP', '43.9506'], ['destination_ip:10.32.0.100 destination_port:80 destination_service:http syslog_priority:Info', '43.9506'], ['destination_ip:10.32.0.100 destination_port:80 destination_service:http protocol:TCP', '43.9506'], ['destination_ip:10.32.0.100 destination_port:80 destination_service:http', '43.9506'], ['destination_ip:10.32.0.100 destination_port:80 syslog_priority:Info protocol:TCP', '43.9506']]

最佳答案

听起来你可以使用集合:

>>> {1, 2, 3, 4, 5} & {2, 3, 4, 5, 6, 7} & {3, 4, 5}
{3, 4, 5}

& 是集合的交集运算符。获取一组列表(这将使用 set(mylist) 删除重复元素。

编辑:根据您的评论,您似乎需要某种联合(联合运算符是 |),而不是交集。这是一个函数,可以在评论中对 2 个列表列表执行您想要的操作:

def function(first, second):
first_set = {tuple(i) for i in first}
second_set = {tuple(i) for i in second}
return (first_set | {(i[0], 0) for i in second_set},
second_set | {(i[0], 0) for i in first_set})

>>> a = [(1,60),(3,90)]
>>> b = [(2,30),(4,50)]
>>> x, y = function(a, b)
>>> print(x)
{(2, 0), (3, 90), (1, 60), (4, 0)}
>>> print(y)
{(3, 0), (4, 50), (1, 0), (2, 30)}

关于python - 查找列表之间的差异并将差异附加到列表,但对于 40 个不同的列表 - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18146660/

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