简体   繁体   English

比较列表元素与多个列表元素

[英]Compare list element with multiple list elements

I have two lists: 我有两个清单:

targets = [ [ [4.88], [2.76], [0.4] ], [ [2.6], [2.12], [7.4], [0.2] ] ]
multiples = [ [ [4.2, 3.6, 6.3], [3.5, 2.5], [7.3, 0.5] ], [ [3.6, 0.3], [5.2, 8.6, 3.7], [3.6, 0.4], [2.3, 6.4] ] ]

For every entry in the first list, there are multiple entries in the second one. 对于第一个列表中的每个条目,第二个列表中有多个条目。 Now I want to compare these numbers and count the accurences of the numbers that are lower than the traget number. 现在,我想比较这些数字,并计算比traget数字低的数字的精确度。 I tried the following, but I don't know how I can compare one single value with multiples and how to refer simultaniously to them. 我尝试了以下方法,但是我不知道如何将一个值与多个值进行比较,以及如何同时引用它们。

for element in targets:
    tmp = []
    for item in element:
        tmp2 = []
        if item > multiples

The output should be something like this: 输出应该是这样的:

[ [ [2], [1], [0] ], [ [1], [0], [2], [0] ] ]

Does somebody know a solution? 有人知道解决方案吗?

One solution using zip() and sum() : 一种使用zip()sum()解决方案:

targets = [ [ [4.88], [2.76], [0.4] ], [ [2.6], [2.12], [7.4], [0.2] ] ]
multiples = [ [ [4.2, 3.6, 6.3], [3.5, 2.5], [7.3, 0.5] ], [ [3.6, 0.3], [5.2, 8.6, 3.7], [3.6, 0.4], [2.3, 6.4] ] ]

out = []
for val1, val2 in zip(targets, multiples):
    out.append([[sum(i < j for i in vval2) for j in vval1] for (vval1, vval2) in zip(val1, val2)])

print(out)

Prints: 印刷品:

[[[2], [1], [0]], [[1], [0], [2], [0]]]

you wrote: 'For every entry in the first list' . 您写道: “对于第一个列表中的每个条目” So i think you have nested your lists to deep... i flattened your lists a bit and did: 所以我认为您已经将您的列表嵌套得更深了……我稍微弄平了您的列表,然后做了:

targets_flat = [
    4.88, 2.76, 0.4,
    2.6, 2.12, 7.4, 0.2
]
multiples_flat = [
    [4.2, 3.6, 6.3], [3.5, 2.5], [7.3, 0.5],
    [3.6, 0.3], [5.2, 8.6, 3.7], [3.6, 0.4], [2.3, 6.4]
]

for ref, valuelist in zip(targets_flat, multiples_flat):
    lower = [v for v in valuelist if ref > v]
    print("reference: {} -> lower: {}".format(ref, lower))

If the deep nested lists are your intention so you must flatten the lists ( see here an example ) 如果您打算使用深层嵌套列表,则必须将列表展平( 请参见此处的示例

In [4]: [[[len([v for v in vals if v < tg[0]])] for vals, tg in zip(mult, tl)] for mult, tl in zip(multiples, targets)]
Out[4]: [[[2], [1], [0]], [[1], [0], [2], [0]]]

The zip built-in function is ideal to iterate over multiple lists. 内置的zip函数非常适合迭代多个列表。 The proposed solution heavily relies on it zipping recursively your lists of lists. 提议的解决方案在很大程度上依赖于它递归压缩列表列表。 While it is a one-liner it is certainly not the most readable solution to your problem. 尽管它是单线的,但它肯定不是解决您问题的最易读的解决方案。

Note : The [] around len() is really just to match with the desired output. 注意len()周围的[]实际上只是与所需的输出匹配。 If you omit it you get: 如果您忽略它,则会得到:

In [5]: [[len([v for v in vals if v < tg[0]]) for vals, tg in zip(mult, tl)] for mult, tl in zip(multiples, targets)]
Out[5]: [[2, 1, 0], [1, 0, 2, 0]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM