Python List Reference (increase, add)

I am trying to connect google geocode api and github api to analyze the user's location and create a list from it.

The array (list) I want to create is as follows:

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

Where the location, lat and lon are analyzed from the Google geocode, it is believed that this place. Every time a new place is added: if it exists in the list, the account increases, otherwise it is added to the array (list) with the location, lat, lon, and the account should be 1.

Another example:

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

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

Can anyone help me? thank.

+2
source share
5

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})
+3

, . , / ( lat/long ):

lat_long_dict = {}
lat_long_dict["San Francisco"] = (x, y)
lat_long_dict["Mumbai"] = (x1, y1)

collections.defaultdict count, 0:

import collections
city_counts = collections.defaultdict(int)

city_counts["San Francisco"] += 1
city_counts["Mumbai"] += 1
city_counts["San Francisco"] += 1
# city counts would be
# defaultdict(<type 'int'>, {'San Francisco': 2, 'Mumbai': 1})
+2

Python : collections.Counter. , (city, lat, lon) (, ), Counter , . ,

>>> locations = [('Miami', 1, 1), ('San Francisco', 2, 2), ('Mumbai', 3, 3), ('Miami', 1, 1), ('Miami', 1, 1)]
>>> Counter(locations)
Counter({('Miami', 1, 1): 3, ('San Francisco', 2, 2): 1, ('Mumbai', 3, 3): 1})

, Counter update.

+1

:

from collections import defaultdict

inputdata = [('Miami', 'x2', 'y2'),
             ('San Francisco', 'x', 'y'),
             ('San Francisco', 'x4', 'y4'),
             ('Mumbai', 'x1', 'y1'),
             ('Cairo', 'x3', 'y3')]

counts, coords = defaultdict(int), defaultdict(list)

for location, lat, lon in inputdata:
    coords[location].append((lat,lon))
    counts[location] += 1

print counts, coords

defaultdict, , , :

  • lat/lon

:

defaultdict(<type 'int'>, {'Miami': 1, 'San Francisco': 2, 'Cairo': 1, 'Mumbai': 1}) 
defaultdict(<type 'list'>, {'Miami': [('x2', 'y2')], 'San Francisco': [('x', 'y'), ('x4', 'y4')], 'Cairo': [('x3', 'y3')], 'Mumbai': [('x1', 'y1')]})

() , lat/lon , .

+1

python dict? .

http://docs.python.org/2/tutorial/datastructures.html#dictionaries

:

// Create an empty dictionary.
dat = {}

if dat.has_key(location):
    dat[location] = dat[location] + 1
else:
    dat[location] = 1
0
source

All Articles