繁体   English   中英

有人可以帮助我解决我认为可能是无限循环的事情吗?

[英]Can anyone help me through what I think might be an infinite loop?

我正在尝试使用以下代码进行某种组检查。 我已经使用组号将人员分配到了组,因此:

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)

我尝试添加注释以帮助指导代码,以防万一我做错了,如果让我分心,我会删除它们,如果可以的话,请提供帮助

我建议将其完全重构并采用其他方法。 您实际上是在将组号映射到人群,因此这听起来像是使用词典的理想之地。 尝试这个:

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灰色3',
“鲍勃·琼斯4”,
'macy grey 6',
“迈克·约翰逊5号”,
'macy grey 6',
'马克安东尼2']

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

请注意,在输入中,有两个实例被分配给组2的“马克·安东尼”,因此一个被撞到了组3。两个“马西灰色”的实例留在了他们所在的位置(组3和6),因为它们在不同的组,但是分配给第六组的第二组又被推到了第0组。现在您有了一个井井有条的字典,而且我认为这段代码更容易理解和维护。 现在,如果出现过多的特定名称,我就提出了一个例外,但是您必须决定如何处理该名称。

我从您的代码中删除了所有多余的东西。 Havent检查了一下。 应该只有小错误。

  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)

暂无
暂无

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

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