我已经通过输入值创建了包含特定单词的列表的函数。 我想从'search'函数中调用'index'。但我有这样的属性错误消息;
if distance.haversine([search.index]['geometry']['coordinates'][1],
[search.index]['geometry']['coordinates'][0],t_dicc['tuits']['coordinates']['latitud'],
t_dicc['tuits']['coordinates']['longitud']<=radius):
**AttributeError: 'function' object has no attribute 'index'**
我尝试多次修改,但没有用。
import os
import string
import json
from pprint import pprint
import distance
def main():
f = open('monumentos-reducido.json', 'r')
mo_dicc = json.load(f)
g = open('tuits.json', 'r')
t_dicc = json.load(g)
def search():
word = raw_input("monument name : ")
if(word in value for word in ('nombre')):
try:
StopIteration
if word > 1:
index = next(index for (index, d) in enumerate(mo_dicc['features']) if d["properties"]["nombre"] == word)
pprint(mo_dicc['features'][index])
except StopIteration:
exit()
search()
radius = input("radius(meters) : ")
def search2rad(search):
resultlst = []
if distance.haversine([search.index]['geometry']['coordinates'][1],[search.index]['geometry']['coordinates'][0], t_dicc['tuits']['coordinates']['latitud'], t_dicc['tuits']['coordinates']['longitud']<=radius):
index = next(index for (index, d) in enumerate(t_dicc['tuits']) if d['coordenadas'])
resultlst.append(t_dicc['tuits'][index])
print resultlst
search2rad(search)
答案 0 :(得分:1)
您正在调用search2rad
函数为search2rad(search)
,其中search
是函数。在search2rad()
内,你正在做:
if distance.haversine([search.index]['geometry']['coordinates'][1],[search.index]['geometry']['coordinates'][0], t_dicc['tuits']['coordinates']['latitud'], t_dicc['tuits']['coordinates']['longitud']<=radius):
您在此提及search.index
。它正在引发错误,因为它是函数(没有属性为index
)。
我认为您要做的是将search()
调用返回的值传递给search2rad()
。为此,您可以这样做:
search2rad(search())
但更清洁的方式就是:
index = search()
search2rad(index)