Python 연습 20: 패턴 찾기
의문
예시
find_pattern({
"Phrase1": "COVID-19 is no good",
"Phrase2": "COVID-18 is no good",
"Phrase3": "COVID-17 is no good"
}, "COVID-19")
➞ ["Phrase1 = COVID-19", "Phrase2 = None", "Phrase3 = None"]
find_pattern({
"Phrase1": "Edabit is great",
"Phrase2": "Edabit is very great",
"Phrase3": "Edabit is really great"
}, "really")
➞ ["Phrase1 = None", "Phrase2 = None", "Phrase3 = really"]
내 솔루션
>>get the value of each Phrase
>>separate the the value into a list by white space
>>store each separated value into list
the list name from phase1_list to phase6_list
>>check if the given string (p) appear in the each phrase
for phrase1 to phrase 6:
if the given string (p) appear in the phrase value
add that phrase and value into one string
add that string to the final list
>>return a final list
def find_pattern(dictionary: dict, string_p: str) -> list:
prefix = "Phrase"
counter = 1
final_list = []
for sentence in dictionary.values():
if string_p in sentence:
final_list.append(f"{prefix}{counter} = {string_p}")
else:
final_list.append(f"{prefix}{counter} = None")
counter += 1
return final_list
기타 솔루션
def find_pattern(dict_, p):
phrase = {True:p,False:"None"}
phrases = []
for key,val in dict_.items():
phrases.append('{} = {}'.format(key,phrase[p in val]))
return sorted(phrases)
def find_pattern(dict_, p):
phrase = {True:p,False:"None"}
phrases = []
for key,val in dict_.items():
phrases.append('{} = {}'.format(key,phrase[p in val]))
return sorted(phrases)
def find_pattern(dct, p):
return sorted("{} = {}".format(d, p if p in dct[d] else None) for d in dct)
신용 거래
Reference
이 문제에 관하여(Python 연습 20: 패턴 찾기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/mathewchan/python-exercise-20find-the-pattern-2i3d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)