任何人都可以帮助我完成我认为可能无限循环的事情吗?

时间:2014-08-08 03:41:49

标签: python django django-admin django-views

我尝试使用下面的代码进行的是一种组检查。我已经使用组号将人员分配到组:

fname lname  group
mark  anthony 2
macy  grey    3      etc..

我想阻止任何两个姓氏相同的人在同一个群组中。所以我写了一个动作尝试这样做,但我想我可能已经发现自己处于一个无限循环中,无论哪种方式都不起作用。

def groupcheck(modeladmin, request, queryset):

    groups=[]
    N = 7       # number of groups
    for X in queryset:
        item = "".join([X.fname, X.lname, str(X.group)])     #the information I am trying to check 

        if item in groups:     # check if it is the array above (groups)
           while item in groups: 
             G = X.group
             Y = int(G) + int(1)    #change the group number
             if Y > N:      # This is my attempt to not allow the group number to be more than the number of groups i allowed (N)
                Y = 0
             removeditemnumber = ''.join([i for i in item if not i.isdigit()]) # I am removing the group number 
             item = removeditemnumber + str(Y)       # now I am trying to replace it
           groups.append(item)     #once the loop is done i want the edited version to be added to the array
           X.group = Y
           X.save()        # I want the new group number to be saved to the databse
        else:
           groups.append(item)

我尝试添加评论以帮助指导代码,以防我做错了,如果分散注意力让我知道,我会删除它们,如果可以请帮助

2 个答案:

答案 0 :(得分:0)

我建议完全重构并采取不同的方法。你实际上是将组号映射到一组人,所以这听起来像是使用字典的完美地方。试试这个:

import pprint

name_list = [
        "mark   anthony 2",
        "macy   grey    3",
        "bob    jones   4",  
        "macy   grey    6",  
        "mike   johnson 5",
        "macy   grey    6",
        "mark   anthony 2"]

pprint.pprint(name_list)

N = 7

# create a dictionary where the keys are gorup numbers
groups = {n:[] for n in xrange(N)}

# this lambda function will increment the input up to N
# then wrap around to zero
increment = lambda x: x+1 if x+1 < N else 0

for name in name_list:
    fname, lname, group_num = name.split()

    group_num = int(group_num)
    old_group = group_num

    # increment the group number while the name is in
    # the current group
    while (fname, lname) in groups[group_num]:
        group_num = increment(group_num)
        if group_num == old_group:
            # if we've checked all the groups and there's
            # no space for this name, raise exception
            raise NoMoreSpaceError

    # we found a spot! add the current name to the current
    # group number
    groups[group_num].append((fname, lname))

pprint.pprint(groups)

输出:

  

['mark anthony 2',
  'macy grey 3',
  'bob jones 4',
  'macy grey 6',
  '迈克约翰逊5',
  'macy grey 6',
  'mark anthony 2']

     

{0:[('macy','grey')],
  1:[],
  2:[('mark','anthony')],
  3:[('macy','grey'),('mark','anthony')],
  4:[('bob','jones')],
  5:[('mike','johnson')],
  6:[('macy','grey')]}

请注意,在输入中,有两个“mark anthony”实例被分配到第2组,因此一个被碰到第3组。两个“macy grey”实例保持原状(第3组和第6组),因为它们属于不同的组,但是分配给第6组的第二个组被碰到了第0组。现在你最终得到了一个有组织的字典,我认为这个代码更易于遵循和维护。就目前而言,如果特定名称的出现次数过多,我就会引发异常,但您必须决定如何处理它。

答案 1 :(得分:0)

我从您的代码中删除了所有额外的东西。没检查过。应该只有轻微的错误。

  groups = []
  N = 7

  for X in queryset:
    item = "".join([X.fname, X.lname, str(X.group)])     
    nCount = 0
    while item in groups:
      X.group = (X.group + 1)%N 
      item = "".join([X.fname, X.lname, str(X.group)])  

      # This part only checks for a very stringent condition. Mostly 
      # unnecessary for real names. Only the couple of lines above
      # are meaningful as far as the code is concerned. 
      nCount += 1
      if nCount > N: 
        print X.fname, X.lname, 'exists in all', N, 'groups ...'
        sys.exit(1)

    groups.append(item)