我是python编程的新手,我正在尝试计算每个国家的机场数量。有人能帮我解决以下问题吗
import csv
f = open("airports.dat")
for lists in csv.reader(f):
airports = lists[1]
countries = lists[3]
for air in airports:
if air not in airports:
airports[air]=1
else:
airports[air]+=1
print(airports)
获取以下错误-
TypeError Traceback (most recent call last)
<ipython-input-75-df427e17a15c> in <module>()
6
7 for air in airports:
----> 8 if air not in airports(airports):
9 airports[air]=1
10 else:
TypeError: 'str' object is not callable
从documentation到
csv.reader
:这意味着下面代码中的
lists
实际上将引用一个列表(不是复数!)字符串数目:这意味着您将字符串分配给
airports
:这又意味着这段代码在字符串中的每个字符上迭代:
由于字符串是一个序列(由字符组成),当您试图用非整数下标值(特别是单个字符)访问该序列的元素时,会得到一个
TypeError
:相关问题 更多 >
编程相关推荐