【问题标题】:How to find the index of last element on list and add it to another如何找到列表中最后一个元素的索引并将其添加到另一个
【发布时间】:2015-11-12 12:19:30
【问题描述】:

我正在开发一个程序,这个程序的意义是什么:

  1. 统计调查中包含的住户数量并以三栏格式打印。

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

  3. 确定收入低于 2015 年美国连续州贫困线的家庭百分比。使用给定的公式计算贫困水平的收入。该公式位于 povertycalc 函数中,该函数分配了名为 povertyLevel 的变量。

问题是这样的,我试图列出另一个低于贫困线的房屋列表的索引列表。

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
3210   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

【问题讨论】:

  • 所以,我应该能够计算它是否在该线以下,如果是,我会怎么做?将该房子添加到列表中以便稍后打印?
  • 您可以在不使用全局变量的情况下进行此操作.. 您应该这样做。不要使用全局变量。通过函数参数传递信息,您的问题应该得到解决。不要使用全局变量......请......

标签: python


【解决方案1】:

首先在你的代码sn-p中,有几个问题。

您可能会想,声明一个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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    相关资源
    最近更新 更多