Python Forum

Full Version: SimplyGIS query point-value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have a question regarding a attribute query I teach myself.

I have three cities:

NAME = 0
POINTS = 1
POP = 2

cities = []
cities.append(["DENVER", [-104.98, 39.74], 634265])
cities.append(["BOULDER", [-105.27, 40.02], 98889])
cities.append(["DURANGO", [-107.88, 37.28], 17069])
As you can see I gave em a name, coordinates(points= longitudinal values, latitude values) and a population-number

Now, I did a attribute query, wanting to know, which is the biggest and western most city:

biggest_city = max(cities, key=lambda city: city[POP])

western_city = min(cities, key=lambda city: city[POINTS])
Now to the problem:


I wanted to know how I can do a query, where I can get the info, what is the most northern city? since I don't know how I can specifically target values inside the POINTS attribute, I wanted to get some help. I thought about making a seperate attribute (POINT1, POINT2) and then perform a query, but since I already have a code which is several hundred lines and has more then a hundred cities in it...yeah.

Secondly, how the hell did my code then work? How did python know, that Durango is the western most city, since the POINT attribute in the city consists of two values.

I hope this is understandably enough, and I'm grateful for any kind of help.
(Feb-10-2021, 09:52 AM)stampy Wrote: [ -> ]Secondly, how the hell did my code then work? How did python know, that Durango is the western most city, since the POINT attribute in the city consists of two values.
You are comparing list, i.e. that is element-wise comparison. It works because it first compares the latitude value and only if there are same values it will compare the second element (i.e. the longitude value). So it will not work if you look for most northern/southern city.
you can specify key=lambda city: city[POINTS][0] to compare only element with index 0.

That said, look at collections.namedtuple. you can have namedtuple for city, as well as namedtuple for coordinates and it will make your code more readable.

from collections import namedtuple

City = namedtuple('City', ['name', 'coordinates', 'population'])
Point = namedtuple("Point", ['latitude', 'longitude'])

cities = []
cities.append(City("DENVER", Point(-104.98, 39.74), 634265))
cities.append(City("BOULDER", Point(-105.27, 40.02), 98889))
cities.append(City("DURANGO", Point(-107.88, 37.28), 17069))

most_populated = max(cities, key=lambda city: city.population)
print(most_populated)
most_northern = max(cities, key=lambda city: city.coordinates.longitude)
print(most_northern)
Output:
City(name='DENVER', coordinates=Point(latitude=-104.98, longitude=39.74), population=634265) City(name='BOULDER', coordinates=Point(latitude=-105.27, longitude=40.02), population=98889)
thanks alot for clearing that up to me, I'm still in the early stages, but I wanted to start somewhere. Have a good day!