Python列表帮助(增加计数,追加) [英] Python list help (incrementing count, appending)

查看:163
本文介绍了Python列表帮助(增加计数,追加)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接谷歌的地理code API和GitHub的API来分析用户的位置,并创建一个列表出来。

I am trying to connect google's geocode api and github api to parse user's location and create a list out of it.

我要创建数组(列表)是这样的:

The array (list) I want to create is like this:

location, lat, lon, count
San Francisco, x, y, 4
Mumbai, x1, y1, 5

在哪里的位置,纬度和经度是从谷歌地球code解析,计数是该位置的发生。 Eevery时间被添加了新的位置。如果它在列表中存在的计数被递增否则,它被附加到阵列(列表)与位置,纬度,经度和计数应为1

Where location, lat and lon is parsed from Google geocode, count is the occurrence of that location. Eevery time a new location is added: if it exists in the list the count is incremented otherwise it is appended to the array(list) with location, lat, lon and the count should be 1.

另外一个例子:

location, lat, lon, count
Miami x2, y2, 1 #first occurrence
San Francisco, x, y, 4 #occurred 4 times already
Mumbai, x1, y1, 5 #occurred 5 times already
Cairo, x3, y3, 1 #first occurrence

我已经可以得到从GitHub用户的位置,可以从谷歌获得地理codeD数据。我只需要创建这个数组中的蟒蛇,我正在与挣扎。

I can already get the user's location from github and can get the geocoded data from google. I just need to create this array in python which I'm struggling with.

谁能帮我?谢谢。

推荐答案

通过 collections.Counter ,你可以这样做:

from collections import Counter

# initial values
c=Counter({("Mumbai", 1, 2):5, ("San Francisco", 3,4): 4})

#adding entries
c.update([('Mumbai', 1, 2)])
print c  # Counter({('Mumbai', 1, 2): 6, ('San Francisco', 3, 4): 4})

c.update([('Mumbai', 1, 2), ("San Diego", 5,6)])
print c  #Counter({('Mumbai', 1, 2): 7, ('San Francisco', 3, 4): 4, ('San Diego', 5, 6): 1})

这篇关于Python列表帮助(增加计数,追加)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆