TypeError:“float”对象不可下标(不知道如何修复)

2024-09-25 00:34:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这个程序运行得很好,但是我收到了一个TypeError: 'float' object is not subscriptable,我不知道如何修复它。你知道吗

这是我的代码:函数读取空气质量数据并返回平均空气质量字典

def make_avg_pm2_dictionary():
    d = {}
    with open("air_data.tsv", encoding ='latin1') as fd:
        for line in fd:
            row=line.strip().split("\t")
            if row[0] in d:
                key = row[0]
                val1 = float(d[row[0]])
                val2 = float(row[6])
                temp = (val1 + val2)/2
                d[key] = round(temp, 2)
            elif row[0] == 'Country':
                val1 = 0
            else:
                d[row[0]] = float(row[6])
    fd.close()
    return d

每个国家的空气质量字典(aqd) 并返回一本字典,里面有每个国家的人口和空气质量 如果那个国家有空气质量数据

def add_cia_population_data(dic1):
    with open("cia_population.tsv", encoding='latin1') as fd:
        for line in fd:
            row = line.strip().split("\t")
            key = row[1]
            if key in dic1:
                temp = [row[2], dic1[key]]
                d = {key: temp}
                dic1.update(d)
        fd.close() 
    return dic1

打印出国家名称、人口和pm2值 超过世界卫生组织规定的1年pm2水平的阈值(单位:微克/立方米) 这将使长期死亡风险从图1增加15% 打印按国家姓氏排序的数据

def print_exceed_threshold(data,threshold):
    for keys in data:
        temp = data[keys]
        if temp[1] >= threshold:
            name = str(keys)
            mp2 = str(temp[1])
            pop = str(temp[0])
            print("{0:<25} {1:<20} {2:<10}".format(name,pop,mp2))

调用所有函数

def main():

# Build dictionary from air quality file
    avg_pm2 = make_avg_pm2_dictionary()
# Read in cia population and create a dictionary
# with population and average pm2 data for each country
    country_data = add_cia_population_data(avg_pm2)
# print countries with air quality
# exceeding WHO's guidelines
    print_exceed_threshold(country_data,35)

#run the analysis


main()

程序应该显示一些统计数据,没有太多。你知道吗

回溯:

Traceback (most recent call last):

  File "<ipython-input-1-6bf5bffb30ed>", line 1, in <module>
    runfile('/Users/felipe/Desktop/A05/A05.py', wdir='/Users/felipe/Desktop/A05')

  File "/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/felipe/Desktop/A05/A05.py", line 82, in <module>
    main()

  File "/Users/felipe/Desktop/A05/A05.py", line 77, in main
    print_exceed_threshold(country_data,35)

  File "/Users/felipe/Desktop/A05/A05.py", line 60, in print_exceed_threshold
    if temp[1] >= threshold:

TypeError: 'float' object is not subscriptable

Tags: keyinpydatathresholdline空气质量float
2条回答

print_exceed_threshold函数中,有一个名为temp的变量,它是一个浮点而不是数组。我会用一些断点或打印重写函数:

def print_exceed_threshold(data, threshold):
    print(threshold, type(threshold)) // to see what this variable is
    for keys in data:
        temp = data[keys]
        print(temp, type(temp))
        # if temp[1] >= threshold:
        #     name = str(keys)
        #     mp2 = str(temp[1])
        #     pop = str(temp[0])
        #     print("{0:<25} {1:<20} {2:<10}".format(name,pop,mp2))

然后您必须返回add_cia_population_data并找到导致键出错的行。也许在dic1.update(d)前面加一个print语句

从外观上看,错误在函数print_exceed_threshold()if temp[1] >= threshold:行。如果temp不是列表,则不能调用temp[1]。你知道吗

相关问题 更多 >