2025/02 58

dict 을 이용한 일괄 문자열 바꿔치기

s = "hello world, have a great day"r_dict = {"hello": "hi", "world": "earth", "great": "wonderful"} 1. Loop 를 이용한 replacefor key, value in r_dict.items():     s = s.replace(key, value) print(s)-> "hi earth, have a wonderful day" 2. 정규표현 re.sub()import re # "스페이스 + ( hello|world|great ) + 스페이스"# lambda match는 매치 될때마다 매치 문자열 오브젝트가 할당 => 문자열은 object.group()# s 는 초기 문자열result = re.sub(r'\b(?:' + '|'..

data science/python 2025.02.12

여러 단어를 한 단어로 바꿔치기

1. 문자열을 스페이스로 분리하여 대체s = 'Macron made the announcement to mark the opening of the Paris AI Summit.' # 바꾸고자 하는 단어들 li = ["announcement", "opening", "Summit."] # 새로운 단어 k = "Trump" # 스페이스로 분리해 단어 리스트를 만들고 바꿀 단어들과 같다면 새로운 단어로 대헤 res = ' '.join([k if word in li else word for word in s.split()]) print(res)-> Macron made the Trump to mark the Trump of the Paris AI Trump 2. 정규표현 '|' 을 이용 import re s = ..

data science/python 2025.02.11

반복되는 특정 단어를 복수 단어로 바꿔치기

시나리오DB 에 LLM의 프롬프트 형식을 아래와 같이 저장해서 나중에 꺼내와서 사용자가 입력한 문자열로 바꾸고자 할때.예) promt = "{input} 은 어느나라의 {input} 입니까?" 방법1prompt = "{input} 은 어느나라의 {input} 입니까?"repl_str = '{input}'repl_list = ['서울', '수도'] for ele in repl_list:     prompt = promt.replace(repl_str, ele, 1) # 서울은 어느나라의 수도 입니까? 방법2import re prompt = "{input} 은 어느나라의 {input} 입니까?" repl_str = '{input}' repl_list = ['서울', '수도']# re.sub 두번째는 lamb..

data science/python 2025.02.11