简体   繁体   English

如何使用枚举计算条件循环中的项目? Python

[英]How to count items in a conditional loop using enumerate? Python

I have list of lists a containing 7 lists, and a list of lists x containing 2 lists.我有a包含 7 个列表的列表列表,以及一个包含 2 个列表的列表x I want to test the lists in a against those in x .我想测试a中的列表与x中的列表。

My goal is to make an item-by-item comparison and find out for how many lists in a all values are larger than the corresponding item in x .我的目标是进行逐项比较,并找出所有值中a多少列表大于x中的相应项目。

A condition and a counter check whether the a's touch the x's.条件和计数器检查 a 是否触及 x。 Note: I am not interested in counting how many items in with a list, a1 for example, touch x1.注意:我对计算列表中有多少项目不感兴趣,例如 a1,touch x1。 Once a1 and x1 touch, I count that and can move on to a2, and so on.一旦 a1 和 x1 接触,我就会数一下,然后就可以移动到 a2,依此类推。

However, the counter does not properly update.但是,计数器没有正确更新。 Any suggestions on how to solve this?关于如何解决这个问题的任何建议? Here is my code.这是我的代码。 The results I expect are shown below the code.我期望的结果显示在代码下方。

EDIT编辑

To clarify the expected result, I have updated the example by adding a second value to x .为了阐明预期结果,我通过向x添加第二个值来更新示例。 So have updated from x = [[10], [14]] to x = [[10, 11], [14, 12]]所以已经从x = [[10], [14]]更新为x = [[10, 11], [14, 12]]

This is updated below.这在下面更新。

x = [[10, 11], [14, 12]]
a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]]

def touch(e, f):
    e = np.array(e)
    f = np.array(f)
    count = []
    counter = 0
    for i, lst in enumerate(e):
        if np.all(f > lst): # This is the condition
            counter += 0 # Counts violations of the condition
            count.append(counter)
            if i == 1:
                counter = 0
        else:
            counter += 1 # Counts violations of the condition
            print(counter)
            count.append(counter)
            if i == 1:
                counter = 0
    return count

touching = touch(x, a)
print(touching)

The result I expect is this:我期望的结果是这样的:

[2, 6]

But I get this:但我明白了:

[1, 2]

EDIT编辑

To clarify the expected result [2, 6] : I am comparing for every list in a and x , item 1 in a to item 1 in x , item 2 in a to item 2 in x .为了澄清预期结果[2, 6] :我正在比较 a 和x中的每个列表, a中的项目 1 与x中的项目 1 , a中的项目 2 与x中的项目 2 进行比较。

So: a1_1 (9) (ie item 1 in list 1 of a ) is lower than x1_1 (10).所以:a1_1 (9)(即 a 的列表 1 中a项目 1)低于 x1_1 (10)。 a1_2 (10) is equal to x1_2 (10) - so that means there are 2 violations of the condition. a1_2 (10) 等于 x1_2 (10) - 这意味着有 2 次违反条件。 a3_1 (11) > x1_1 (10) and a3_2 (12) > x1_2 (11) and the other lists in a are also higher than their corresponding elements. a3_1 (11) > x1_1 (10) 和 a3_2 (12) > x1_2 (11) 以及 a 中的其他列表也高于其对应的元素。 For x2 (2nd list in x):, all lists in a are lower except a7, in which a7_1 (15) is higher than x2_1 and a7_2 is higher than x2_2.对于 x2(x 中的第二个列表):, a所有列表都较低,除了 a7,其中 a7_1 (15) 高于 x2_1,a7_2 高于 x2_2。 Hence [2, 6] .因此[2, 6]

I can't quite follow your reasoning in your code, but you can check a against x in a single list comprehension:我不能完全按照您在代码中的推理,但是您可以在单个列表理解中检查 a 与 x :

x = [[10], [14]]
a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]]

def touch(x, a):
    return [[ all([asel > xel[0] for asel in ael]) for ael in a].count(False) for xel in x]
touching = touch(x, a)
print(touching)

>>> [2,6]

Depending on what you really need, you can change the > condition to >= or count either False or True values.根据您的实际需要,您可以将>条件更改为>=或计算FalseTrue值。 If the inner lists of x contain multiple element instead of one given in the example, you need to loop the inner lists as well:如果 x 的内部列表包含多个元素而不是示例中给出的一个,则还需要循环内部列表:

x = [[10, 11], [14, 12]]
a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]]

def touch(x, a):
    return [[all([asel > xsubel for xsubel in xel for asel in ael]) for ael in a].count(False) for xel in x ]
touching = touch(x, a)
print(touching)

You can use Numpy to avoid any for loops.您可以使用 Numpy 来避免任何 for 循环。 In the code below I assumed that the list x was 1-D by the way, since it seems the sublists it contains consist of 1 element.在下面的代码中,我假设列表x是一维的,因为它包含的子列表似乎由 1 个元素组成。

import numpy as np
x = [10,14]
a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]]

e = np.array(x)
f = np.array(a)
touching = (np.sum(np.any(f[...,np.newaxis] <= e, axis = 1), axis = 0))
 
print(touching)

I have a different perspective.我有不同的看法。 In python, you can see that:在 python 中,您可以看到:

>>> [1, 1] > [1, 2]
False
>>> [1, 1] > [1, 1]
False
>>> [1, 1] > [1, 0]
True
>>> [2, 3] > [3, 3]
False
>>> [2, 3] < [3, 3]
True
>>> [2, 2] > [0, 2]
True

So, we can compare entire lists.因此,我们可以比较整个列表。 First flatten the x and, then compare x with element in a首先将x展平,然后将x与 a 中的元素进行a

x = [[10], [14]]
a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]]

def touch(e, f):
    e = [q for p in e for q in p] # [10, 14]
    return sum([1 for sub_list in f if sub_list > e])

print(touch(x, a))

Here is a simple solution without any need for numpy这是一个简单的解决方案,不需要numpy

x = [[10], [14]]
a = [[9, 10], [10, 11], [11, 12], [12, 13], [13, 14], [14, 15], [15, 16]]

def touch(e, f):
    # We can keep only maximum element of each list
    e = [max(lst) for lst in e]
    count = [0]*len(e)
    for lst in f:
        # Even the minimum must be greater
        min_value = min(lst)
        for i in range(len(e)):
            if min_value>e[i]:
                count[i] += 1
    return count

touching = touch(x, a)
print(touching)
# [5, 1]

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

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