Python 多次迭代筛选对象';得不到正确的值

Python 多次迭代筛选对象';得不到正确的值,python,Python,以下代码得到正确答案,因为在分配给b之前调用了list()最大值(b)如果删除列表,可能会得到错误的值 a = map(lambda x: (x[0]*10 + x[1], x[2]*10 + x[3]), aLongList) b = list(filter(lambda x: x[0] < 24 and x[1] < 60, a)) # convert to a list if not any(b): #.... omitted max(b) a=map(λx:(x[0

以下代码得到正确答案,因为在分配给
b
之前调用了
list()
<代码>最大值(b)如果删除
列表
,可能会得到错误的值

a = map(lambda x: (x[0]*10 + x[1], x[2]*10 + x[3]), aLongList)
b = list(filter(lambda x: x[0] < 24 and x[1] < 60, a)) # convert to a list
if not any(b):
    #.... omitted
max(b)
a=map(λx:(x[0]*10+x[1],x[2]*10+x[3]),以及列表)
b=列表(过滤器(λx:x[0]<24和x[1]<60,a))#转换为列表
如果没有(b):
#.... 省略
最高(b)
这是一种在不创建列表的情况下获得正确结果的方法吗(如果列表很大)


测试:

下面的代码

def perm(A):
    for i,x in enumerate(A):
        for j,y in enumerate(A):
            if i == j: continue
            for k,z in enumerate(A):
                if i == k or j == k: continue
                for l,a in enumerate(A):
                    if l == i or l == j or l == k: continue
                    yield [A[i], A[j], A[k], A[l]]

a = map(lambda x: (x[0]*10 + x[1], x[2]*10 + x[3]), perm([1,9,6,0]))
b = (filter(lambda x: x[0] < 24 and x[1] < 60, a)) # Removed list, so b is a filter obj instead of list

if not any(b):
    pass
max(b)
def perm(A):
对于枚举(A)中的i,x:
对于枚举(A)中的j,y:
如果i==j:继续
对于枚举(A)中的k,z:
如果i==k或j==k:继续
对于枚举(a)中的l,a:
如果l==i或l==j或l==k:继续
收益率[A[i]、A[j]、A[k]、A[l]]
a=map(λx:(x[0]*10+x[1],x[2]*10+x[3]),perm([1,9,6,0]))
b=(filter(lambda x:x[0]<24和x[1]<60,a))#删除了列表,因此b是一个过滤器对象,而不是列表
如果没有(b):
通过
最高(b)

返回
(16,9)
而不是
(19,6)

使用
itertools.tee

从单个iterable返回n个独立迭代器


max(filter(lambda x:x[0]<24和x[1]<60,a))
这对我来说很有用,它需要调用filter对象上的
any()
max()
from itertools import tee

b1, b2 = tee(filter(lambda x: x[0] < 24 and x[1] < 60, a), 2)

if not any(b1):
    pass
max(b2)
(19, 6)