I'm wondering if there's a more efficient way to do this in Python. I found a good solution in Ruby but it seems fairly specific.
Basically, I'm fetching weather condition data from an API and want to standardize their many nuanced conditions down to seven I can easily deal with them.
def standardize_weather_conditions(s):
clear_chars = ['clear', 'sunny']
clouds_chars = ['cloudy', 'overcast', 'fog']
storm_chars = ['thunder']
freezing_chars = ['ice', 'sleet', 'freezing rain', 'freezing drizzle']
snow_chars = ['snow', 'blizzard']
rain_chars = ['rain', 'drizzle', 'mist']
if any_in_string(s, clear_chars):
conditions = 'clear'
elif any_in_string(s, clouds_chars):
conditions = 'clouds'
elif any_in_string(s, storm_chars):
conditions = 'storm'
elif any_in_string(s, freezing_chars):
conditions = 'freezing'
elif any_in_string(s, snow_chars):
conditions = 'snow'
elif any_in_string(s, wet_chars):
conditions = 'wet'
else:
conditions = 'other'
return conditions
def any_in_string(s, array):
for e in array:
if e in s:
return True
return False