簡體   English   中英

如何使用枚舉計算條件循環中的項目? Python

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

我有a包含 7 個列表的列表列表,以及一個包含 2 個列表的列表x 我想測試a中的列表與x中的列表。

我的目標是進行逐項比較,並找出所有值中a多少列表大於x中的相應項目。

條件和計數器檢查 a 是否觸及 x。 注意:我對計算列表中有多少項目不感興趣,例如 a1,touch x1。 一旦 a1 和 x1 接觸,我就會數一下,然后就可以移動到 a2,依此類推。

但是,計數器沒有正確更新。 關於如何解決這個問題的任何建議? 這是我的代碼。 我期望的結果顯示在代碼下方。

編輯

為了闡明預期結果,我通過向x添加第二個值來更新示例。 所以已經從x = [[10], [14]]更新為x = [[10, 11], [14, 12]]

這在下面更新。

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)

我期望的結果是這樣的:

[2, 6]

但我明白了:

[1, 2]

編輯

為了澄清預期結果[2, 6] :我正在比較 a 和x中的每個列表, a中的項目 1 與x中的項目 1 , a中的項目 2 與x中的項目 2 進行比較。

所以:a1_1 (9)(即 a 的列表 1 中a項目 1)低於 x1_1 (10)。 a1_2 (10) 等於 x1_2 (10) - 這意味着有 2 次違反條件。 a3_1 (11) > x1_1 (10) 和 a3_2 (12) > x1_2 (11) 以及 a 中的其他列表也高於其對應的元素。 對於 x2(x 中的第二個列表):, a所有列表都較低,除了 a7,其中 a7_1 (15) 高於 x2_1,a7_2 高於 x2_2。 因此[2, 6]

我不能完全按照您在代碼中的推理,但是您可以在單個列表理解中檢查 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]

根據您的實際需要,您可以將>條件更改為>=或計算FalseTrue值。 如果 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)

您可以使用 Numpy 來避免任何 for 循環。 在下面的代碼中,我假設列表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)

我有不同的看法。 在 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

因此,我們可以比較整個列表。 首先將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))

這是一個簡單的解決方案,不需要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