Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表的筛选索引_Python_Python 3.x_List_Iterator_Iterable - Fatal编程技术网

Python 列表的筛选索引

Python 列表的筛选索引,python,python-3.x,list,iterator,iterable,Python,Python 3.x,List,Iterator,Iterable,我有以下数字清单: List = [1, 2, 3, 4, 5, 6, 15] 我想要这些数字的索引是n的倍数,所以我要: def indexes(List, n): # to enumerate the numbers E = enumerate(List) # filtering tuples F = list(filter(lambda x: x[1] % n == 0, E)) return [ i[0] for i in

我有以下数字清单:

List = [1, 2, 3, 4, 5, 6, 15]
我想要这些数字的索引是n的倍数,所以我要:

def indexes(List, n):
      # to enumerate the numbers
      E = enumerate(List)
      # filtering tuples
      F = list(filter(lambda x:  x[1] % n == 0, E))
      return [ i[0] for i in F]

     indexes(List, 2)
     [1, 3, 5]
没关系,但是当我添加变量m时会发生什么

def Index( L, n, m):
      # enumeration
      E = enumerate(L)
      # filtering tuples
      F_n = list(filter(lambda x: x[1]%n == 0, E))
      F_m = list(filter(lambda x: x[1]%m == 0, E))
      L_n = [ l[0] for l in F_n]
      L_m = [ J[0] for J in F_m]
      return L_n + L_m

  >>>Index(List, 2, 5):
        [1, 3, 5]
为什么该代码不返回[1,3,5,4,6]

错在哪里

以及如何创建返回该列表的函数?

您可以将列表理解与
枚举
方法结合使用。 此外,还可以应用运算符,以便根据需要传递多个参数

List = [1, 2, 3, 4, 5, 6, 15]
def indexes(List, *vars):
   return [index for index, item in enumerate(List) for i in vars if item % i == 0 ]

print(indexes(List, 2, 5))
输出

[1, 3, 5, 4, 6]

问题是
枚举
。一旦用完,就不能再使用了。因此,您可以简单地定义一个新的
枚举
迭代器:

lst = [1, 2, 3, 4, 5, 6, 15]

def Index( L, n, m):

    # enumeration - notice we define 2 objects
    E, F = enumerate(L), enumerate(L)

    F_n = list(filter(lambda x: x[1]%n == 0, E))
    F_m = list(filter(lambda x: x[1]%m == 0, F))
    L_n = [ l[0] for l in F_n]
    L_m = [ J[0] for J in F_m]
    return L_n + L_m

res = Index(lst, 2, 5)

print(res)
[1, 3, 5, 4, 6]

请注意,有更好的方法可以实现算法。

一种更通用、更适合任何数量变量的python方法是使用
any()
all()
函数检查所有参数的条件真值。如果您希望索引属于可整除的项,请购买所需的所有参数
all()
其他方面,您可以使用
any()
在遇到匹配项后立即返回True

def indexes(lst, *args):
    return [i for i, j in enumerate(lst) if any(j % arg == 0 for arg in args)]
演示:

使用
all()


关于
list(filter(lambda x:x[1]%n==0或x[1]%m==0,E))
?通过等待,请注意filter()和列表理解或多或少是相同的,尤其是在您的情况下。考虑使用列表理解,这是更为可读的,我不知道的是,OR运算符可以加入两个列表,感谢这是一个更pythic的方式去在这种情况下,但不会是整洁的变量的数量增加。
>>> lst = [1, 2, 3, 4, 5, 6, 15, 99, 200, 13, 17, 400]

>>> indexes(lst, 99, 5, 2, 100)
[1, 3, 4, 5, 6, 7, 8, 11]
>>> 
>>> indexes(lst, 5, 2, 100)
[8, 11]