返回后继续遍历子列表吗?

问题描述:

我试图通过值列表的列表迭代,看看最里面的值遵守一定的规则:返回后继续遍历子列表吗?

  • 空白列表返回([],“空白”)
  • 名单负整数,浮点数,或字符串返回([],'非数字')
  • 列表与正整数和返回的组合。

问题是,在返回子列表中的第一个值之后,程序将跳过该子列表中的所有其他值,然后转到下一个值,查看当前输出以进一步查看我的意思是。

def getPapers(f, n): 
    x = f.readlines() #read f to x with \n chars and whitespace 
    strippedPaper = [line.strip("\n").replace(' ', '') for line in x] #stores formatted ballot 
    #print(strippedPaper) 

    print() 

    #Create list of lists from ballots. 
    strippedBallot = [item.split(",") for item in strippedPaper] 
    #print(strippedBallot) 
    print() 


    #Information passed to parsePaper 
    try: 
     for ballot in strippedBallot: 
      print(ballot) #Show individual ballots 
      valueParsePaper = parsePaper(ballot, n) #ballot is passed to parsePaper here. 
      print(valueParsePaper) #Displays the value returned from parsePaper 


    except TypeError: 
     print("There was an error with the data type passed to var 'valueParsePaper'\n" 
       "from this set.\n")  


def parsePaper(s, n): 
    #If s is informal reutrn an empty list with an appropriate string 
    try: 
     if len(s) > n: 
      tooLong = ([], "too long") 
      return tooLong 
     elif len(s) == 0: 
      blankBallot = ([], "blank") 
      return blankBallot 
     #elif sum(s[:]) == 0: 
      #blankBallot = ([], "blank") 
      #return blankBallot 

     else:    
      voteWorth = parseVote(s) 
      #The vote inside the ballot is passed to parseVote 

      #parseVote returns a value to voteWorth. 

      if voteWorth == 0: #for empty/blank string 

       return ([], "blank") 

      elif voteWorth == int(-1): #for non-digits/invalid numbers 
       return ([], "non-digits") 

      else: #for valid votes 
       return ([s], "") 

    except ValueError: 
     print("There is an invalid value at parsePaper") 


#parseVote(s) Returns vote from s, return 0 for empty vote, -1 for votes containing non-digits 
#except spaces 

def parseVote(s): 
    try: 
     for i in s: 
      if i == ' ': 
       return int(0) #for empty spaces, return to parsePaper 

      elif i == '': 
       return int(0) #for blanks, return to parsePaper 

      elif i.isdigit() == False: #for non-digits 
       return int(-1) #return to parsePaper 

      elif int(i) < 0: #for negnative numbers 
       return int(-1) #return to parsePaper 

      else: 
       return int(i) #return the positive integer to parsePaper 
    except ValueError: 
      print("The object passed to parseVote is invalid.") 

这表明在对所述输入和输出。

[''] 
([], 'blank') 

['1', '2', '3', '4'] 
([['1', '2', '3', '4']], '') 

['', '23', ''] 
([], 'blank') 

['9', '-8'] 
([['9', '-8']], '') 

['thesepeople!'] 
([], 'non-digits') 

['4', '', '4', '4'] 
([['4', '', '4', '4']], '') 

['5', '5', '', '5', '5'] 
([['5', '5', '', '5', '5']], '') 

前两行是细,它是空白,并返回出现很好,因为它返回值作为空白,接下来的两行,但所述第三对不应该返回([],空白),因为输入包含正整数。你也可以看到第4对应该返回'非数字',因为它包含一个负数。

经过一步一步,我发现该函数只返回每个子列表的第一个值。

我需要什么是程序再次通过相同的子列表,检查每一个价值判断之前,如果一个子表是有效的 - 我不能确定如何检查每个子列表值然后确定该子列表的全部内容是否有效。

+2

请提供[mcve]。你的代码包含很多不相关的部分。以示例输入和示例输出开始,然后在示例中重现您的错误。 –

+0

感谢您的阅读,对不起,我无法提供一个更简单的例子来解决这个问题。我相信我现在已经解决了我自己的问题,所以我在下面分享我的答案。 –

我想我已经找到了解决我自己的问题的人谁也遇到过:

通过检查和追加交给parseVote()每票到一个新的列表,然后通过新的列表清单回到parsePaper(),我已经能够做出我需要的调整。

现在包含文本或负数的选票会自动失效,选票总计为零或空白。

def getPapers(f, n): 
    x = f.readlines() #read f to x with \n chars and whitespace 
    strippedPaper = [line.strip("\n").replace(' ', '') for line in x] #stores formatted ballot 
    #print(strippedPaper) 

    print() 

    #Create list of lists from ballots. 
    strippedBallot = [item.split(",") for item in strippedPaper] 
    #print(strippedBallot) 
    print() 

    #Information passed to parsePaper 
    try: 
     for ballot in strippedBallot: 
      print(ballot) #Show individual ballots 
      valueParsePaper = parsePaper(ballot, n) #Ballot is passed to parsePaper here. 
      print(valueParsePaper) #print returned value 


    except TypeError: 
     print("There was an error with the data type passed to var 'valueParsePaper'\n" 
       "from this set.\n")  


def parsePaper(s, n): 
    #If s is informal reutrn an empty list with an appropriate string 
    try: 
     if len(s) > n: 
      tooLong = ([], "too long") 
      return tooLong 
     elif len(s) == 0: 
      blankBallot = ([], "blank") 
      return blankBallot 
     #elif sum(s[:]) == 0: 
      #blankBallot = ([], "blank") 
      #return blankBallot 

     else:    
      voteWorth = parseVote(s) 
      #The vote inside the ballot is passed to parseVote 

      #parseVote returns a value to voteWorth. 

      if voteWorth == 0: #for empty/blank string 

       return ([], "blank") 

      elif voteWorth == int(-1): #for non-digits/invalid numbers 
       return ([], "non-digits") 

      else: #for valid votes 
       return (voteWorth, "") 

    except ValueError: 
     print("There is an invalid value at parsePaper") 


#parseVote(s) Returns vote from s, return 0 for empty vote, -1 for votes containing non-digits 
#except spaces 

def parseVote(s): 
    try: 
     voteWorthList = [] 
     for i in s: 
      if i == ' ': 
       i = 0 
       voteWorthList.append(i) 
       #return int(0) #for empty spaces, return to parsePaper 

      elif i == '': 
       i = 0 
       voteWorthList.append(i) 
       #return int(0) #for blanks, return to parsePaper 

      elif i.isdigit() == False: #for non-digits 
       i = int(-1) 
       voteWorthList.append(i) 
       #return int(-1) #return to parsePaper 

      elif int(i) < 0: #for negnative numbers 
       i = int(-1) 
       voteWorthList.append(i) 
       #return int(-1) #return to parsePaper 

      else: 
       i = int(i) 
       voteWorthList.append(i) 
       #return int(i) #return the positive integer to parsePaper 
     print(voteWorthList) 

     for i in voteWorthList: 
      if i < 0: 
       return int(-1) 

     if sum(voteWorthList) == 0: 
       return 0 
     else: 
      return voteWorthList 

    except ValueError: 
      print("The object passed to parseVote is invalid.") 

新的输出是这样的,显示了原始输入,调整后的输入,并最终输出:

[''] 
[0] 
([], 'blank') 

['1', '2', '3', '4'] 
[1, 2, 3, 4] 
([1, 2, 3, 4], '') 

['', '23', ''] 
[0, 23, 0] 
([0, 23, 0], '') 

['9', '-8'] 
[9, -1] 
([], 'non-digits') 

['thesepeople!'] 
[-1] 
([], 'non-digits') 

['4', '', '4', '4'] 
[4, 0, 4, 4] 
([4, 0, 4, 4], '') 

['5', '5', '', '5', '5'] 
[5, 5, 0, 5, 5] 
([5, 5, 0, 5, 5], '') 

下面是它是否会帮助一个较短的版本。我希望这有助于我正确理解你的问题。基本上,你可以在一个功能,而不是做任何事情3

def getPapers(f, n): 
    x = f.readlines() 
    strippedPaper = [line.strip("\n").replace(' ', '') for line in x] 
    ballot = tuple() 
    allBlanks = False if [i.strip() for i in strippedPaper if i] else True 
    if allBlanks: 
     ballot = ([], "blank") 
    elif len(strippedPaper) > n: 
     ballot = ([], "too long") 
    else: 
     ballot = (strippedPaper, "") 
     for i in strippedPaper: 
      if i.strip() == '': 
       continue 
      if not i.isdigit() or int(i) < 0: 
       ballot = ([], "non-digits") 
       break 

    return ballot