Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t对python库中的Google Places[';评级';]数据进行排序_Python_Google Maps_Google Places Api - Fatal编程技术网

Can';t对python库中的Google Places[';评级';]数据进行排序

Can';t对python库中的Google Places[';评级';]数据进行排序,python,google-maps,google-places-api,Python,Google Maps,Google Places Api,当我在做不同的测试时,我发现了一个问题,当我调用google.places()时,我无法按['rating].value对数据进行排序。 例如,如果我查询数据并按['name']进行排序,它的工作就非常好。 示例如下: gmaps = googlemaps.Client(key='key_is_here') nyc = (40.6971494, -74.2598655) msk = (55.7535081, 37.6144299) new_arr = [] params = { 'qu

当我在做不同的测试时,我发现了一个问题,当我调用google.places()时,我无法按['rating].value对数据进行排序。 例如,如果我查询数据并按['name']进行排序,它的工作就非常好。 示例如下:

gmaps = googlemaps.Client(key='key_is_here')
nyc = (40.6971494, -74.2598655)
msk = (55.7535081, 37.6144299)
new_arr = []

params = {
    'query': ['restaurants', 'bakery', 'cafe', 'food'],
    'location': nyc
    'radius': 10000
}


gmapz = gmaps.places(**params)
print sorted(gmapz['results'], key = lambda x : x['name'],reverse = True):
        """ outputs list of dicts """
然而,当我在lambda键中使用['raiting']时,我会感到惊讶

    Traceback (most recent call last):
  File "/Users/Aleshka/Desktop/teszz.py", line 22, in <module>
    print sorted(gmapz['results'],key = lambda x : x['rating'],reverse=True)
  File "/Users/Aleshka/Desktop/teszz.py", line 22, in <lambda>
    print sorted(gmapz['results'],key = lambda x : x['rating'],reverse=True)
KeyError: 'rating'
我也真的很困惑,因为几天前我在纽约(确切地说是曼哈顿)也能做到这一点,但当我在不同的地方做了几次测试,选择了俄罗斯的莫斯科时,结果失败了。不知道出了什么问题。
我认为这可能发生,因为我达到了查询限制,但事实并非如此。

可能有任何一个结果都没有关键的“评级”。 尝试通过打印没有键“rating”的结果来调试它

for res in gmapz['results']:
    if 'rating' not in res.keys():
        print res

现在您知道哪些结果没有任何评分。

在@Shra1的帮助下,我解决了这个问题

print sorted(gmapz['results'], key = lambda x: ('rating' not in x, `x.get('rating',None)),reverse = True)`

显示没有评级的第一个项目,其余项目按降序排序。

如果我像打印gmapz['results'][0]['rating']那样进行,它会显示第一名的评级。我现在更困惑了,我觉得我当时没有做足够的测试。事实确实如此。
print sorted(gmapz['results'], key = lambda x: ('rating' not in x, `x.get('rating',None)),reverse = True)`