파이썬에서 더 효율적인 방법이 있는지 궁금합니다. I found a good solution in Ruby하지만 상당히 구체적인 것으로 보입니다.배열의 요소가 주어진 문자열의 부분 문자열인지 어떻게 알 수 있습니까?
기본적으로 API에서 기상 조건 데이터를 가져오고 있으며 7 가지로 뉘앙스 조건을 표준화하여 쉽게 처리 할 수 있습니다.
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
두 번째 제안이 정말 마음에 듭니다. 어딘가에 맞을 수도 있습니다. 또한, 실제로 방금 비슷한 솔루션을 생각해 냈습니다. (그냥'any_in_string' 코드를 함수 호출 대신에 리터럴에 넣었습니다.) – delwin