如何找到名单上的最后一个元素的索引,并将其添加到另一个

问题描述:

我工作的一个程序,什么该方案的要点是:如何找到名单上的最后一个元素的索引,并将其添加到另一个

  1. 计数的户数纳入调查并以三列格式打印。

  2. 计算平均家庭收入,并列出每个家庭的识别号码和收入超过平均水平。

  3. 确定收入低于2015年美国连续国家贫困水平的家庭百分比。使用给出的公式计算贫困水平收入。该公式在贫穷Calcal功能分配变量称为贫穷水平

问题是这个,即时通讯试图做一个房屋的另一列表下降到贫困线以下的指标列表。

def main(): 
    NumberofHouseholds = 0 
    inFile = open('program9.txt', 'r') 
    HouseIncome = [] 
    global HouseIncome 
    avgincome = 0 

    lineRead = inFile.readline()  # Read first record 
    while lineRead != '':    # While there are more records 
     words = lineRead.split()  # Split the records into substrings 
     acctNum = int(words[0])   # Convert first substring to integer 
     annualIncome = float(words[1]) # Convert second substring to float 
     members = int(words[2])   # Convert third substring to integer 
     global members 
     global annualIncome 
     print(acctNum, format(annualIncome, '.2f'), members, sep=' ') 
     HouseIncome.append(annualIncome) 
     povertycalc() 
     NumberofHouseholds = NumberofHouseholds + 1 
     avgincome = (avgincome+annualIncome)/NumberofHouseholds 
     lineRead = inFile.readline() # Read next record 

    # Close the file. 
    inFile.close() # Close file 
    print(avgincome) 
    print(HouseIncome) 

def povertycalc(): 
    indexKeep = [] 
    global indexKeep 
    m = members 
    povertyLevel = 15930.00 + 4160.00 * (m-2) 
    if annualIncome < povertyLevel: 
     indexKeep.append(HouseIncome.index(HouseIncome[-1])) #Finds most recent index of household in HouseIncome and adds it to list indexKeep 
    print(indexKeep) #not printing the new value added to indexKeep 

# Call the main function. 
main() 

当我昨天通过电子邮件发送我的教授我被告知:

“你不能用另一个列表的数据作为指标。”

我该怎么做才能让这个效率更高?截至目前,该计划打印出给出的信息的三列(我将发布与该计划相关的TXT文件的链接),以及包含贫困线以下房屋列表的空白支架。

下面是与该计划相关的文件中的文本:

都在你的代码片段的
1042 12180.06 3 
1062 13240.45 2 
1327 19800.56 2 
1483 22458.23 7 
1900 17000.09 3 
2112 18125 4 
2345 15623 2 
3200 1 
3600 39500 5 
3601 11970 2 
4724 8900 3 
6217 45000.70 2 
9280 6200 1 
1000 31000 3 
1200 36000 2 
5601 51970 9 
5724 66900 3 
5217 10002.68 2 
5280 70000 1 
5000 100000 6 
5200 25000.4 3 
5230 120000 6 
6641 85000 7 
7000 45500 4 
7100 56500 3 
8110 110005.9 8 
9101 67590.40 6 
+0

所以,我应该能够计算出它是否在线以下,如果它是我会做什么?将房子添加到列表中以便日后打印? – user232978

+0

你可以在不使用全局变量的情况下去解决这个问题。你应该。不要使用全局变量。通过函数参数传递信息并解决您的问题。不要使用全局变量....请.... –

首先,一些问题在那里。

您可能会认为声明global变量会使该变量成为该脚本的global !.但实际并非如此 您错过了关键字global。 通过使用global语句,您可以告诉python解释器您要访问/修改外部作用域中的变量,而不是将其分配给本地。 然而,尽量少用global声明是一个很好的编程设计。

在代码片段indexKeep.append(HouseIncome.index(HouseIncome[-1]))中,HouseIncome[-1]提取列表的最后一个值。因此,合并HouseIncome.index(HouseIncome[-1])给出了最后一个元素的索引,您正在间接地处理它。相反,您可以简单地找到len(HouseIncome) - 1以获得相同的结果。但您所指的HouseIncome是您在顶部初始化为空的那个。所以最终不会起作用。

我修改了一下你的程序。但我没有测试过它。可能你想看看那个。

def main(): 
    NumberofHouseholds = 0 
    HouseIncome = [] 
    avgincome = 0 

    for idNumber, row in enumerate(open('program9.txt', 'r')): # While there are more records 
     words = row.split()  # Split the records into substrings 
     acctNum = int(words[0])   # Convert first substring to integer 
     annualIncome = float(words[1]) # Convert second substring to float 
     members = int(words[2])   # Convert third substring to integer 
     print(acctNum, format(annualIncome, '.2f'), members, sep=' ') 
     if povertycalc(annualIncome, idNumber + 1): 
      HouseIncome.append(annualIncome) 
     NumberofHouseholds = NumberofHouseholds + 1 
     avgincome = (avgincome+annualIncome)/NumberofHouseholds 

    # Close the file. 
    print(avgincome) 
    print(HouseIncome) 

def povertycalc(annualIncome, members): 
    povertyLevel = 15930.00 + 4160.00 * (members-2) 

    if annualIncome < povertyLevel: 
     return True 
    else: return False 

# Call the main function. 
main()