以下哪个操作更快?
使用 for 循环:
OrgIdChoices = []
Orgid_used_choices = [(choice["organization_id"])for choice in list(Organization.objects.all().values("organization_id"))] #Gets used ID's
OrgIdAvailChoices = [ "%c%c" % (x, y) for x in range(ord('A'), ord('Z')+1) for y in range(ord('A'), ord('Z')+1)] #Gets available ID's
for i in OrgIdAvailChoices:
if not i in Orgid_used_choices:
OrgIdChoices.append((i,i)) #Generates OrgIdAvailChoices which are not in Orgid_used_choices
或使用集合操作:
OrgIdChoices = []
Orgid_used_choices = set([(choice["organization_id"])for choice in list(Organization.objects.all().values("organization_id"))])
OrgIdAvailChoices = set([ "%c%c" % (x, y) for x in range(ord('A'), ord('Z')+1) for y in range(ord('A'), ord('Z')+1)])
OrgChoices = OrgIdAvailChoices - Orgid_used_choices
for i in OrgChoices:
OrgIdChoices.append((i,i))