简体   繁体   English

知道在Python中的for循环中是否还有任何要迭代的元素的更好方法

[英]Better way to know if there are any elements left to iterate in for loop in Python

I have the following for loop: 我有以下for循环:

def validate(request, fields=None):
    i = 0
    j = len(fields)
    return_fields = []

    for f in fields:
        i += 1
        if request.data[f] != check_validity(f)
            if i == j:
                return_fields.append(f)
                return return_fields
            else:
                return_fields.append(f)

This checks the validity of the fields by some function check_validity(). 这通过某些函数check_validity()检查字段的有效性。 If it is invalid, it appends it to the return_fields list and at the end, returns it to the main caller. 如果无效,则将其附加到return_fields列表中,最后将其返回给主调用方。 My question is, in my code, I am using two variables i and j to just check if there are any elements left in the list 'fields'. 我的问题是,在我的代码中,我使用两个变量i和j只是检查列表“字段”中是否还有任何元素。 But I am just wondering if there is a better way to do the same operation since I want to return all the invalid fields and not stop when I encounter any 1 of them. 但是我只是想知道是否有更好的方法来执行相同的操作,因为我想返回所有无效字段,并且在遇到其中任何一个无效字段时都不要停止。

Another way is to use enumerate : 另一种方法是使用枚举

def validate(request, fields=None):
    return_fields = []

    for i, f in enumerate(fields):
        if request.data[f] != check_validity(f)
            if i+1 == len(fields):
                return_fields.append(f)
                return return_fields
            else:
                return_fields.append(f)

But i don't get it, why you need to know it? 但是我不明白,为什么你需要知道它? Why you can't just: 为什么你不能只是:

def validate(request, fields=None):
    for f in fields:
        if request.data[f] != check_validity(f)
            return_fields.append(f)
    return return_fields

Short form: 简写:

def validate(request, fields=None):
    return [
        f for f in fields
        if request.data[f] != check_validity(f)
    ]

A for loop automatically finishes when it's iterated all the way through an array, so you can just take the return out of the for loop and it'll run once all the elements in fields have been iterated through. for循环在整个数组中进行迭代时会自动完成,因此您只需将return从for循环中取出,一旦对字段中的所有元素进行了迭代,它将立即运行。

def validate(request, fields=None):
    return_fields = []

    for f in fields:
        if request.data[f] != check_validity(f)
            return_fields.append(f)
return return_fields
def validate(request, fields=None):
    return_fields = []
    buggy = False
    for f in fields:
        if request.data[f] != check_validity(f)
            buggy = True
            return_fields.append(f)
    if buggy: return return_fields

BTW in your code, it will return return_fields only when last field is invalid. 顺便说一句,在您的代码中,仅当最后一个字段无效时,它才会返回return_fields。

I guess using yield will be of use for you :) (take a look here What does the "yield" keyword do in Python? , it is relatively complex, but it is like a return without restarting the function) 我想使用yield将对您有用:)(在这里看看“ yield”关键字在Python中有什么用? ,它相对复杂,但是就像在不重新启动函数的情况下一样)

def validate(request, fields=None):
   for f in fields:
        if request.data[f] != check_validity(f):
            yield (f)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM