2

I have a condition that I'm checking against each element in a list and if the condition becomes true than I want to increment the value of a counter in one of several variables. Here's my example:

for el in elements:
    if sum(x, el) < 5:
        .........DO SOMETHING: Increment variable age

Now if I want to increment the variable that relates to that el in elements how would I do it. So for example if the el was age and then I want to increment an age variable how would this be done?

I could do this by writing lots of switch statements, but what would be the most pythonic way to do it.

Thanks

EDIT

What I'm looking to do is that each element is a set of coordinates of a city(Long, Lat) and for each one I'm calculating the distance from an event. I'll be using geopy.

Now as each element is coordinates not a city name. I want to say if the event is less than 5km from the coordinates of New York to increment the New York variable or if it's 5km from London to increment the London variable.

Hope this makes it more clearer.

3
  • 1
    Could you provide an example of your input list and perhaps a counter or two? From your example, it looks like you're iterating through a list of numbers, so it is tough to see where the distinction between age and another variable would come into play. Do you have another list that specifies which particular index position relates to a particular variable? Commented Dec 2, 2012 at 20:37
  • How is your 'comparison' set organized? For instance, is it a dictionary that contains a city and its coordinates with a corresponding (lat, long) (e.g. {'New York': (x, y)}), and if so, where/how are you storing your counter variables? Same structure or elsewhere? Commented Dec 2, 2012 at 21:02
  • I was thinking of storing them in another list. Could I store them in the dict like you have above as further value for the New York key and then iterate through the dict and if condition is true increment the variable. Would it be possible in a dictionary data structure? Commented Dec 2, 2012 at 21:19

1 Answer 1

5

This is a case for a list comprehension:

[el if sum(x, el) < 5 else el + 1 for el in elements]

Edit: From your edit, I'm not really sure what you are describing matches what you said originally, what your edit says matches something like this more closely:

cities = {"London": (10, 15), "New York": (20, 50)}
def find_closest_city(location):
    ...

city_count = collections.Counter([find_closest_city(el) for el in elements])
Sign up to request clarification or add additional context in comments.

6 Comments

Is it possible to make multiple statements for (let's say) the true-condition; e.g. change two variables if the if-statement is true?
@poplitea You would have to give an example situation, but yes, it's possible to do very complicated things with methods like this if needs be (generally the answer lies in the itertools module).
@Lattyware, my edit hopefully made it clearer from the original. So the function find_closest_city(location) will then iterate through all the coordinates values in cities and if the condition is true, then how do you ensure that the relevent city's counter has been incremented? As in where would there variable counters be stored?
@user94628 The `collections.Counter()`` object is a dictionary-like object of city name to the count for each city.
Cool...I'm relatively new to Python. HAdn't heard of collections.counter So each time it finds a city nearby it increments the count eg. city_count{'New York': 3, 'London': 6, 'Los Angeles' : 7}. Would it then not be: city_count = collections.Counter([find_closest_city(el) for el in cities])?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.