簡體   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