Contact: aviboots(AT)netvision.net.il
39,939 questions
51,876 answers
573 users
import numpy as np lst = [43, 21, 67, 21, 30, 18, 21, 19, 21, 21] lst = np.array(lst) result = np.where(lst == 21) print(result) print(result[0]) ''' run: (array([1, 3, 6, 8, 9]),) [1 3 6 8 9] '''
lst = [43, 21, 67, 21, 30, 18, 21, 19, 21, 21] pos = [] n = 21 for i in range(len(lst)): if lst[i] == n: pos.append(i) print(pos) ''' run: [1, 3, 6, 8, 9] '''
lst = [43, 21, 67, 21, 30, 18, 21, 19, 21, 21] n = 21 pos = [i for i in range(len(lst)) if lst[i] == n] print(pos) ''' run: [1, 3, 6, 8, 9] '''
lst = [43, 21, 67, 21, 30, 18, 21, 19, 21, 21] n = 21 pos = [i for i, x in enumerate(lst) if x == n] print(pos) ''' run: [1, 3, 6, 8, 9] '''
import numpy as np lst = [43, 21, 67, 21, 30, 18, 21, 19, 21, 21] n = 21 result = np.where(np.array(lst) == n)[0] print(result) ''' run: [1 3 6 8 9] '''
from more_itertools import locate lst = [43, 21, 67, 21, 30, 18, 21, 19, 21, 21] n = 21 result = list(locate(lst, lambda x: x == n)) print(result) ''' run: [1 3 6 8 9] '''