2016-09-02 8 views
1

リストの内容が指定された数値(第1のしきい値)よりも大きいかどうかを調べるきれいな方法を探しています一定回数(第2の閾値)。両方のステートメントが真の場合、指定したしきい値を超える最初の値のインデックスを返したいと思います。リストの値が一定のしきい値を超えているかどうかチェックして最初の超過インデックスを返します

# Set first and second threshold 
thr1 = 4 
thr2 = 5 

# Example 1: Both thresholds exceeded, looking for index (3) 
list1 = [1, 1, 1, 5, 1, 6, 7, 3, 6, 8] 

# Example 2: Only threshold 1 is exceeded, no index return needed 
list2 = [1, 1, 6, 1, 1, 1, 2, 1, 1, 1] 
+3

例1が2番目のしきい値を超えていません。ちょうど5つの数字> 4 –

+0

@Chris_Rands彼のスレッショルドは包括的です –

+1

私は同じことを指摘しましたが、OPは代わりに「超過」と「到達」を使用して、彼が望むものを推測するのは難しいです。 – polku

答えて

3

事実を乱用するニシキヘビと考えられていた場合、私は知りませんブール値は整数ですが、このようにするのが好きです。

def check(l, thr1, thr2): 
    c = [n > thr1 for n in l] 
    if sum(c) >= thr2: 
     return c.index(1) 
+0

短く、シンプルで速い!投稿していただきありがとうございます!これを基本とします! (そして他のすべてのおかげで、それらのすべてがうまく機能します!) – NumbThumb

0

ナイーブかつ直接的なアプローチが第1の閾値よりも大きいアイテムの数をカウントし、カウントが超えた場合に最初の一致のインデックスを返すリストを反復処理することであろう第二の閾値:

def answer(l, thr1, thr2): 
    count = 0 
    first_index = None 
    for index, item in enumerate(l): 
     if item > thr1: 
      count += 1 
      if not first_index: 
       first_index = index 

      if count >= thr2: # TODO: check if ">" is required instead 
       return first_index 

thr1 = 4 
thr2 = 5 

list1 = [1, 1, 1, 5, 1, 6, 7, 3, 6, 8] 
list2 = [1, 1, 6, 1, 1, 1, 2, 1, 1, 1] 

print(answer(list1, thr1, thr2)) # prints 3 
print(answer(list2, thr1, thr2)) # prints None 

これは、しかし、おそらくかなりニシキヘビはありませんが、この解決策は、利点のカップルを持っている - 私たちは最初のマッチのインデックスを保つであり、第2の閾値に達した場合にはのループから早めに出口がになります。

つまり、最悪の場合はO(k)、最悪の場合はO(n)となります。ここで、kは第2のしきい値に達する前の項目の数です。 nは、入力リスト内の項目の合計数です。

0

私はきれいかニシキヘビそれを呼びたい場合、私は知りませんが、これは

def get_index(list1, thr1, thr2): 
    cnt = 0 
    first_element = 0 
    for i in list1: 
     if i > thr1: 
      cnt += 1 
      if first_element == 0: 
       first_element = i 

    if cnt > thr2: 
     return list1.index(first_element) 
    else: 
     return "criteria not met" 
2

はこの試してみてください動作するはずです:

def check_list(testlist) 
    overages = [x for x in testlist if x > thr1] 
    if len(overages) >= thr2: 
     return testlist.index(overages[0]) 
    # This return is not needed. Removing it will not change 
    # the outcome of the function. 
    return None 

これは、あなたがif文を使用することができるという事実を使用していますが重要でない値を無視するためのリスト内包表記です。

コメントでChris_Randsが述べたように、return Noneは不要です。これを削除しても、関数の結果は変更されません。

+0

'return testlist.index(overages [0])' –

+0

@ Ev.Kounisああ、ありがとう。質問のインデックス部分を逃した。 – FamousJameous

+0

'return None'は不要です、とにかくPythonは' return None'を返します –

0
thr1 = 4 
thr2 = 5 
list1 = [1, 1, 1, 5, 1, 6, 7, 3, 6, 8] 
list2 = [1, 1, 6, 1, 1, 1, 2, 1, 1, 1] 

def func(lst) 
    res = [ i for i,j in enumerate(lst) if j > thr1] 
    return len(res)>=thr2 and res[0] 

出力:

func(list1) 
3 
func(list2) 
false 
+0

@ Ev.Kounisそれはインデックスです – galaxyan

+0

いいです!申し訳ありません –

0

あなたはワンライナーを探して(あるいはほとんど)している場合は

a = filter(lambda z: z is not None, map(lambda (i, elem) : i if elem>=thr1 else None, enumerate(list1))) 
print a[0] if len(a) >= thr2 else false 
関連する問題