문자열과 리스트 고급 활용 - 실전 메서드들
5.2 문자열과 리스트 고급 활용 - 실전 메서드들
🎯 오늘 배울 내용
지금까지 기본적인 문자열과 리스트를 사용했다면, 이제 실무에서 자주 쓰이는 고급 메서드들을 배워보겠습니다!
📚 이전 내용 간단 요약
Chapter 2~5.1에서 배운 기초:
# 문자열 기초
name = "김코딩"
print(name)
# 리스트 기초
students = ["김철수", "이영희", "박민수"]
students.append("최지영")
# 딕셔너리 (Chapter 5.1)
scores = {"김철수": 85, "이영희": 92}
# 문자열 기초
name = "김코딩"
print(name)
# 리스트 기초
students = ["김철수", "이영희", "박민수"]
students.append("최지영")
# 딕셔너리 (Chapter 5.1)
scores = {"김철수": 85, "이영희": 92}
오늘 목표: 이것들을 더 효율적으로 다루는 방법! 🚀
🔍 PART 1: 문자열 고급 메서드
문자열 분리와 합치기
Chat에 질문:
문자열을 쪼개거나 여러 문자열을 합치는
유용한 방법들을 알려주세요.
문자열을 쪼개거나 여러 문자열을 합치는
유용한 방법들을 알려주세요.
기본 문자열 메서드들:
# 1. 문자열 분리하기 (.split())
sentence = "사과,바나나,포도,딸기"
fruits = sentence.split(",")
print(fruits) # ['사과', '바나나', '포도', '딸기']
# 2. 문자열 합치기 (.join())
words = ["안녕", "하세요", "파이썬"]
result = " ".join(words)
print(result) # "안녕 하세요 파이썬"
# 3. 문자열 바꾸기 (.replace())
text = "파이썬은 어려워요"
new_text = text.replace("어려워요", "재미있어요")
print(new_text) # "파이썬은 재미있어요"
# 4. 앞뒤 공백 제거 (.strip())
messy_text = " hello world "
clean_text = messy_text.strip()
print(f"'{clean_text}'") # 'hello world'
# 1. 문자열 분리하기 (.split())
sentence = "사과,바나나,포도,딸기"
fruits = sentence.split(",")
print(fruits) # ['사과', '바나나', '포도', '딸기']
# 2. 문자열 합치기 (.join())
words = ["안녕", "하세요", "파이썬"]
result = " ".join(words)
print(result) # "안녕 하세요 파이썬"
# 3. 문자열 바꾸기 (.replace())
text = "파이썬은 어려워요"
new_text = text.replace("어려워요", "재미있어요")
print(new_text) # "파이썬은 재미있어요"
# 4. 앞뒤 공백 제거 (.strip())
messy_text = " hello world "
clean_text = messy_text.strip()
print(f"'{clean_text}'") # 'hello world'
🎯 실습 1: 사용자 입력 처리하기
print("=== 깔끔한 사용자 입력 처리 ===")
# 사용자가 여러 과목을 입력하는 상황
user_input = input("좋아하는 과목들을 쉼표로 구분해서 입력하세요: ")
# 입력 처리 (공백 제거 + 분리)
subjects = user_input.split(",")
# 각 과목의 앞뒤 공백 제거
clean_subjects = []
for subject in subjects:
clean_subject = subject.strip()
clean_subjects.append(clean_subject)
print(f"\n입력받은 과목들:")
for i in range(len(clean_subjects)):
print(f"{i+1}. {clean_subjects[i]}")
# 다시 하나의 문자열로 합치기
result = " / ".join(clean_subjects)
print(f"\n정리된 결과: {result}")
print("=== 깔끔한 사용자 입력 처리 ===")
# 사용자가 여러 과목을 입력하는 상황
user_input = input("좋아하는 과목들을 쉼표로 구분해서 입력하세요: ")
# 입력 처리 (공백 제거 + 분리)
subjects = user_input.split(",")
# 각 과목의 앞뒤 공백 제거
clean_subjects = []
for subject in subjects:
clean_subject = subject.strip()
clean_subjects.append(clean_subject)
print(f"\n입력받은 과목들:")
for i in range(len(clean_subjects)):
print(f"{i+1}. {clean_subjects[i]}")
# 다시 하나의 문자열로 합치기
result = " / ".join(clean_subjects)
print(f"\n정리된 결과: {result}")
문자열 확인 메서드
Chat에 요청:
문자열이 숫자인지, 영어인지, 비어있는지 등을
확인하는 방법들을 알려주세요.
문자열이 숫자인지, 영어인지, 비어있는지 등을
확인하는 방법들을 알려주세요.
# 문자열 상태 확인 메서드들
test_strings = ["12345", "abc", "ABC", "Hello123", " ", ""]
for text in test_strings:
print(f"'{text}' 분석:")
print(f" 숫자인가? {text.isdigit()}")
print(f" 영어인가? {text.isalpha()}")
print(f" 영어+숫자인가? {text.isalnum()}")
print(f" 대문자인가? {text.isupper()}")
print(f" 소문자인가? {text.islower()}")
print(f" 공백인가? {text.isspace()}")
print(f" 비어있는가? {len(text) == 0}")
print()
# 문자열 상태 확인 메서드들
test_strings = ["12345", "abc", "ABC", "Hello123", " ", ""]
for text in test_strings:
print(f"'{text}' 분석:")
print(f" 숫자인가? {text.isdigit()}")
print(f" 영어인가? {text.isalpha()}")
print(f" 영어+숫자인가? {text.isalnum()}")
print(f" 대문자인가? {text.isupper()}")
print(f" 소문자인가? {text.islower()}")
print(f" 공백인가? {text.isspace()}")
print(f" 비어있는가? {len(text) == 0}")
print()
🎯 실습 2: 비밀번호 검증기
print("=== 비밀번호 검증기 ===")
password = input("새 비밀번호를 입력하세요: ")
print(f"\n비밀번호 '{password}' 분석:")
# 길이 확인
if len(password) >= 8:
print("✅ 길이: 8자 이상 (통과)")
else:
print("❌ 길이: 8자 미만 (실패)")
# 숫자 포함 확인
has_number = False
for char in password:
if char.isdigit():
has_number = True
break
if has_number:
print("✅ 숫자: 포함됨 (통과)")
else:
print("❌ 숫자: 포함되지 않음 (실패)")
# 영어 포함 확인
has_alpha = False
for char in password:
if char.isalpha():
has_alpha = True
break
if has_alpha:
print("✅ 영어: 포함됨 (통과)")
else:
print("❌ 영어: 포함되지 않음 (실패)")
# 최종 판정
if len(password) >= 8 and has_number and has_alpha:
print("\n🎉 비밀번호가 안전합니다!")
else:
print("\n⚠️ 비밀번호를 더 안전하게 만들어주세요.")
print("=== 비밀번호 검증기 ===")
password = input("새 비밀번호를 입력하세요: ")
print(f"\n비밀번호 '{password}' 분석:")
# 길이 확인
if len(password) >= 8:
print("✅ 길이: 8자 이상 (통과)")
else:
print("❌ 길이: 8자 미만 (실패)")
# 숫자 포함 확인
has_number = False
for char in password:
if char.isdigit():
has_number = True
break
if has_number:
print("✅ 숫자: 포함됨 (통과)")
else:
print("❌ 숫자: 포함되지 않음 (실패)")
# 영어 포함 확인
has_alpha = False
for char in password:
if char.isalpha():
has_alpha = True
break
if has_alpha:
print("✅ 영어: 포함됨 (통과)")
else:
print("❌ 영어: 포함되지 않음 (실패)")
# 최종 판정
if len(password) >= 8 and has_number and has_alpha:
print("\n🎉 비밀번호가 안전합니다!")
else:
print("\n⚠️ 비밀번호를 더 안전하게 만들어주세요.")
🔍 PART 2: 리스트 고급 메서드
리스트 조작 메서드
Chat에 질문:
리스트에서 항목을 제거하거나, 정렬하거나,
찾는 고급 방법들을 알려주세요.
리스트에서 항목을 제거하거나, 정렬하거나,
찾는 고급 방법들을 알려주세요.
리스트 고급 메서드들:
# 리스트 고급 조작
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print(f"원본: {numbers}")
# 1. 정렬하기
numbers.sort()
print(f"정렬 후: {numbers}")
# 2. 역순 정렬
numbers.sort(reverse=True)
print(f"역순 정렬: {numbers}")
# 3. 특정 값 제거 (.remove())
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
numbers.remove(1) # 첫 번째 1만 제거
print(f"1 제거 후: {numbers}")
# 4. 인덱스로 제거 (.pop())
removed = numbers.pop(0) # 첫 번째 항목 제거
print(f"첫 번째 제거: {numbers}, 제거된 값: {removed}")
# 5. 값의 위치 찾기 (.index())
position = numbers.index(4)
print(f"4의 위치: {position}")
# 6. 개수 세기 (.count())
numbers = [1, 2, 1, 3, 1, 4]
count = numbers.count(1)
print(f"1의 개수: {count}")
# 리스트 고급 조작
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
print(f"원본: {numbers}")
# 1. 정렬하기
numbers.sort()
print(f"정렬 후: {numbers}")
# 2. 역순 정렬
numbers.sort(reverse=True)
print(f"역순 정렬: {numbers}")
# 3. 특정 값 제거 (.remove())
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
numbers.remove(1) # 첫 번째 1만 제거
print(f"1 제거 후: {numbers}")
# 4. 인덱스로 제거 (.pop())
removed = numbers.pop(0) # 첫 번째 항목 제거
print(f"첫 번째 제거: {numbers}, 제거된 값: {removed}")
# 5. 값의 위치 찾기 (.index())
position = numbers.index(4)
print(f"4의 위치: {position}")
# 6. 개수 세기 (.count())
numbers = [1, 2, 1, 3, 1, 4]
count = numbers.count(1)
print(f"1의 개수: {count}")
🎯 실습 3: 성적 관리 고급 기능
print("=== 성적 관리 고급 시스템 ===")
# 학생들의 성적 리스트
scores = [85, 92, 78, 95, 88, 76, 89, 94, 82, 91]
print(f"원본 성적: {scores}")
# 1. 성적 정렬 (높은 순)
sorted_scores = scores.copy() # 원본 보존
sorted_scores.sort(reverse=True)
print(f"성적 순위: {sorted_scores}")
# 2. 평균 계산
total = 0
for score in scores:
total += score
average = total / len(scores)
print(f"평균: {average:.1f}점")
# 3. 최고점, 최저점
highest = max(scores)
lowest = min(scores)
print(f"최고점: {highest}점, 최저점: {lowest}점")
# 4. 등급별 분류
grade_a = [] # 90점 이상
grade_b = [] # 80점 이상
grade_c = [] # 나머지
for score in scores:
if score >= 90:
grade_a.append(score)
elif score >= 80:
grade_b.append(score)
else:
grade_c.append(score)
print(f"\nA등급 ({len(grade_a)}명): {grade_a}")
print(f"B등급 ({len(grade_b)}명): {grade_b}")
print(f"C등급 ({len(grade_c)}명): {grade_c}")
# 5. 특정 점수 이상 학생 비율
excellent_count = 0
for score in scores:
if score >= 90:
excellent_count += 1
excellent_rate = (excellent_count / len(scores)) * 100
print(f"\n우수학생 비율: {excellent_rate:.1f}%")
print("=== 성적 관리 고급 시스템 ===")
# 학생들의 성적 리스트
scores = [85, 92, 78, 95, 88, 76, 89, 94, 82, 91]
print(f"원본 성적: {scores}")
# 1. 성적 정렬 (높은 순)
sorted_scores = scores.copy() # 원본 보존
sorted_scores.sort(reverse=True)
print(f"성적 순위: {sorted_scores}")
# 2. 평균 계산
total = 0
for score in scores:
total += score
average = total / len(scores)
print(f"평균: {average:.1f}점")
# 3. 최고점, 최저점
highest = max(scores)
lowest = min(scores)
print(f"최고점: {highest}점, 최저점: {lowest}점")
# 4. 등급별 분류
grade_a = [] # 90점 이상
grade_b = [] # 80점 이상
grade_c = [] # 나머지
for score in scores:
if score >= 90:
grade_a.append(score)
elif score >= 80:
grade_b.append(score)
else:
grade_c.append(score)
print(f"\nA등급 ({len(grade_a)}명): {grade_a}")
print(f"B등급 ({len(grade_b)}명): {grade_b}")
print(f"C등급 ({len(grade_c)}명): {grade_c}")
# 5. 특정 점수 이상 학생 비율
excellent_count = 0
for score in scores:
if score >= 90:
excellent_count += 1
excellent_rate = (excellent_count / len(scores)) * 100
print(f"\n우수학생 비율: {excellent_rate:.1f}%")
🔍 PART 3: 실전 활용 - 텍스트 처리
🎯 실습 4: 단어 분석기
Chat에 요청:
텍스트에서 단어를 분석하고 통계를 내는
프로그램을 만들어주세요.
텍스트에서 단어를 분석하고 통계를 내는
프로그램을 만들어주세요.
print("=== 텍스트 분석기 ===")
# 분석할 텍스트
text = """
파이썬은 정말 재미있는 프로그래밍 언어입니다.
파이썬으로 다양한 프로그램을 만들 수 있어요.
코딩을 배우면 창의적인 아이디어를 구현할 수 있습니다.
파이썬과 함께 멋진 프로젝트를 시작해보세요!
"""
print(f"원본 텍스트:\n{text}")
# 1. 문장으로 분리
sentences = text.split(".")
clean_sentences = []
for sentence in sentences:
clean = sentence.strip()
if len(clean) > 0:
clean_sentences.append(clean)
print(f"\n문장 개수: {len(clean_sentences)}")
for i in range(len(clean_sentences)):
print(f"{i+1}. {clean_sentences[i]}")
# 2. 단어로 분리
all_words = []
for sentence in clean_sentences:
words = sentence.split()
for word in words:
# 특수문자 제거
clean_word = word.replace("!", "").replace("?", "").replace(",", "")
all_words.append(clean_word)
print(f"\n총 단어 개수: {len(all_words)}")
# 3. 단어 빈도 분석
word_count = {}
for word in all_words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
print(f"\n단어 빈도 분석:")
for word in word_count:
count = word_count[word]
if count >= 2: # 2번 이상 나온 단어만
print(f"'{word}': {count}번")
# 4. 가장 많이 나온 단어 찾기
max_count = 0
most_common_word = ""
for word in word_count:
if word_count[word] > max_count:
max_count = word_count[word]
most_common_word = word
print(f"\n가장 많이 나온 단어: '{most_common_word}' ({max_count}번)")
print("=== 텍스트 분석기 ===")
# 분석할 텍스트
text = """
파이썬은 정말 재미있는 프로그래밍 언어입니다.
파이썬으로 다양한 프로그램을 만들 수 있어요.
코딩을 배우면 창의적인 아이디어를 구현할 수 있습니다.
파이썬과 함께 멋진 프로젝트를 시작해보세요!
"""
print(f"원본 텍스트:\n{text}")
# 1. 문장으로 분리
sentences = text.split(".")
clean_sentences = []
for sentence in sentences:
clean = sentence.strip()
if len(clean) > 0:
clean_sentences.append(clean)
print(f"\n문장 개수: {len(clean_sentences)}")
for i in range(len(clean_sentences)):
print(f"{i+1}. {clean_sentences[i]}")
# 2. 단어로 분리
all_words = []
for sentence in clean_sentences:
words = sentence.split()
for word in words:
# 특수문자 제거
clean_word = word.replace("!", "").replace("?", "").replace(",", "")
all_words.append(clean_word)
print(f"\n총 단어 개수: {len(all_words)}")
# 3. 단어 빈도 분석
word_count = {}
for word in all_words:
if word in word_count:
word_count[word] += 1
else:
word_count[word] = 1
print(f"\n단어 빈도 분석:")
for word in word_count:
count = word_count[word]
if count >= 2: # 2번 이상 나온 단어만
print(f"'{word}': {count}번")
# 4. 가장 많이 나온 단어 찾기
max_count = 0
most_common_word = ""
for word in word_count:
if word_count[word] > max_count:
max_count = word_count[word]
most_common_word = word
print(f"\n가장 많이 나온 단어: '{most_common_word}' ({max_count}번)")
🔍 PART 4: 데이터 정리와 변환
🎯 실습 5: CSV 데이터 처리하기
print("=== CSV 데이터 처리기 ===")
# CSV 형식의 학생 데이터 (쉼표로 구분)
csv_data = """
이름,수학,영어,과학
김철수,85,90,88
이영희,92,87,95
박민수,78,82,80
최지영,95,93,91
정수호,88,85,89
"""
print("원본 CSV 데이터:")
print(csv_data)
# 1. 줄별로 분리
lines = csv_data.strip().split("\n")
# 2. 헤더와 데이터 분리
header = lines[0].split(",")
print(f"헤더: {header}")
# 3. 학생 데이터 처리
students_data = []
for i in range(1, len(lines)):
line = lines[i]
data = line.split(",")
# 학생 정보 딕셔너리 생성
student = {
"이름": data[0],
"수학": int(data[1]),
"영어": int(data[2]),
"과학": int(data[3])
}
students_data.append(student)
# 4. 학생별 성적 분석
print(f"\n📊 학생별 성적 분석:")
for student in students_data:
name = student["이름"]
math = student["수학"]
english = student["영어"]
science = student["과학"]
# 총점과 평균 계산
total = math + english + science
average = total / 3
print(f"{name}: 총점 {total}점, 평균 {average:.1f}점")
# 5. 과목별 평균 계산
math_total = 0
english_total = 0
science_total = 0
student_count = len(students_data)
for student in students_data:
math_total += student["수학"]
english_total += student["영어"]
science_total += student["과학"]
print(f"\n📈 과목별 평균:")
print(f"수학: {math_total / student_count:.1f}점")
print(f"영어: {english_total / student_count:.1f}점")
print(f"과학: {science_total / student_count:.1f}점")
# 6. 최고 성적 학생 찾기
best_student = students_data[0]
best_total = students_data[0]["수학"] + students_data[0]["영어"] + students_data[0]["과학"]
for student in students_data:
total = student["수학"] + student["영어"] + student["과학"]
if total > best_total:
best_total = total
best_student = student
print(f"\n🏆 최고 성적: {best_student['이름']} ({best_total}점)")
print("=== CSV 데이터 처리기 ===")
# CSV 형식의 학생 데이터 (쉼표로 구분)
csv_data = """
이름,수학,영어,과학
김철수,85,90,88
이영희,92,87,95
박민수,78,82,80
최지영,95,93,91
정수호,88,85,89
"""
print("원본 CSV 데이터:")
print(csv_data)
# 1. 줄별로 분리
lines = csv_data.strip().split("\n")
# 2. 헤더와 데이터 분리
header = lines[0].split(",")
print(f"헤더: {header}")
# 3. 학생 데이터 처리
students_data = []
for i in range(1, len(lines)):
line = lines[i]
data = line.split(",")
# 학생 정보 딕셔너리 생성
student = {
"이름": data[0],
"수학": int(data[1]),
"영어": int(data[2]),
"과학": int(data[3])
}
students_data.append(student)
# 4. 학생별 성적 분석
print(f"\n📊 학생별 성적 분석:")
for student in students_data:
name = student["이름"]
math = student["수학"]
english = student["영어"]
science = student["과학"]
# 총점과 평균 계산
total = math + english + science
average = total / 3
print(f"{name}: 총점 {total}점, 평균 {average:.1f}점")
# 5. 과목별 평균 계산
math_total = 0
english_total = 0
science_total = 0
student_count = len(students_data)
for student in students_data:
math_total += student["수학"]
english_total += student["영어"]
science_total += student["과학"]
print(f"\n📈 과목별 평균:")
print(f"수학: {math_total / student_count:.1f}점")
print(f"영어: {english_total / student_count:.1f}점")
print(f"과학: {science_total / student_count:.1f}점")
# 6. 최고 성적 학생 찾기
best_student = students_data[0]
best_total = students_data[0]["수학"] + students_data[0]["영어"] + students_data[0]["과학"]
for student in students_data:
total = student["수학"] + student["영어"] + student["과학"]
if total > best_total:
best_total = total
best_student = student
print(f"\n🏆 최고 성적: {best_student['이름']} ({best_total}점)")
💪 종합 실습: 영화 리뷰 분석기
🎯 최종 미션: 영화 리뷰를 분석하는 프로그램
print("🎬 영화 리뷰 분석기")
print("=" * 50)
# 영화 리뷰 데이터
reviews = [
"이 영화는 정말 재미있었어요! 강력 추천합니다.",
"스토리가 좋고 배우들 연기도 훌륭해요.",
"별로였어요. 지루하고 재미없었습니다.",
"최고의 영화! 감동적이고 재미있어요.",
"그냥 그래요. 나쁘지도 좋지도 않은 영화.",
"정말 재미있는 영화였어요. 다시 보고 싶어요!"
]
print("📝 수집된 리뷰:")
for i in range(len(reviews)):
print(f"{i+1}. {reviews[i]}")
# 1. 긍정/부정 키워드 분석
positive_words = ["재미있", "좋", "훌륭", "최고", "추천", "감동"]
negative_words = ["별로", "지루", "재미없", "나쁘"]
positive_count = 0
negative_count = 0
print(f"\n📊 감성 분석:")
for i in range(len(reviews)):
review = reviews[i]
# 리뷰별 감성 점수 계산
review_positive = 0
review_negative = 0
for word in positive_words:
if word in review:
review_positive += 1
for word in negative_words:
if word in review:
review_negative += 1
# 전체 감성 분류
if review_positive > review_negative:
sentiment = "긍정"
positive_count += 1
elif review_negative > review_positive:
sentiment = "부정"
negative_count += 1
else:
sentiment = "중립"
print(f"리뷰 {i+1}: {sentiment}")
# 2. 전체 감성 통계
neutral_count = len(reviews) - positive_count - negative_count
print(f"\n📈 전체 감성 통계:")
print(f"긍정: {positive_count}개 ({positive_count/len(reviews)*100:.1f}%)")
print(f"부정: {negative_count}개 ({negative_count/len(reviews)*100:.1f}%)")
print(f"중립: {neutral_count}개 ({neutral_count/len(reviews)*100:.1f}%)")
# 3. 자주 사용된 단어 분석
all_words = []
for review in reviews:
words = review.split()
for word in words:
# 특수문자 제거
clean_word = word.replace("!", "").replace(".", "").replace(",", "")
if len(clean_word) >= 2: # 2글자 이상만
all_words.append(clean_word)
# 단어 빈도 계산
word_frequency = {}
for word in all_words:
if word in word_frequency:
word_frequency[word] += 1
else:
word_frequency[word] = 1
print(f"\n🔤 자주 사용된 단어 (2번 이상):")
for word in word_frequency:
count = word_frequency[word]
if count >= 2:
print(f"'{word}': {count}번")
# 4. 최종 평가
if positive_count > negative_count:
print(f"\n🎉 결론: 이 영화는 대체로 좋은 평가를 받고 있습니다!")
elif negative_count > positive_count:
print(f"\n😔 결론: 이 영화는 대체로 아쉬운 평가를 받고 있습니다.")
else:
print(f"\n😐 결론: 이 영화에 대한 평가가 엇갈리고 있습니다.")
print("🎬 영화 리뷰 분석기")
print("=" * 50)
# 영화 리뷰 데이터
reviews = [
"이 영화는 정말 재미있었어요! 강력 추천합니다.",
"스토리가 좋고 배우들 연기도 훌륭해요.",
"별로였어요. 지루하고 재미없었습니다.",
"최고의 영화! 감동적이고 재미있어요.",
"그냥 그래요. 나쁘지도 좋지도 않은 영화.",
"정말 재미있는 영화였어요. 다시 보고 싶어요!"
]
print("📝 수집된 리뷰:")
for i in range(len(reviews)):
print(f"{i+1}. {reviews[i]}")
# 1. 긍정/부정 키워드 분석
positive_words = ["재미있", "좋", "훌륭", "최고", "추천", "감동"]
negative_words = ["별로", "지루", "재미없", "나쁘"]
positive_count = 0
negative_count = 0
print(f"\n📊 감성 분석:")
for i in range(len(reviews)):
review = reviews[i]
# 리뷰별 감성 점수 계산
review_positive = 0
review_negative = 0
for word in positive_words:
if word in review:
review_positive += 1
for word in negative_words:
if word in review:
review_negative += 1
# 전체 감성 분류
if review_positive > review_negative:
sentiment = "긍정"
positive_count += 1
elif review_negative > review_positive:
sentiment = "부정"
negative_count += 1
else:
sentiment = "중립"
print(f"리뷰 {i+1}: {sentiment}")
# 2. 전체 감성 통계
neutral_count = len(reviews) - positive_count - negative_count
print(f"\n📈 전체 감성 통계:")
print(f"긍정: {positive_count}개 ({positive_count/len(reviews)*100:.1f}%)")
print(f"부정: {negative_count}개 ({negative_count/len(reviews)*100:.1f}%)")
print(f"중립: {neutral_count}개 ({neutral_count/len(reviews)*100:.1f}%)")
# 3. 자주 사용된 단어 분석
all_words = []
for review in reviews:
words = review.split()
for word in words:
# 특수문자 제거
clean_word = word.replace("!", "").replace(".", "").replace(",", "")
if len(clean_word) >= 2: # 2글자 이상만
all_words.append(clean_word)
# 단어 빈도 계산
word_frequency = {}
for word in all_words:
if word in word_frequency:
word_frequency[word] += 1
else:
word_frequency[word] = 1
print(f"\n🔤 자주 사용된 단어 (2번 이상):")
for word in word_frequency:
count = word_frequency[word]
if count >= 2:
print(f"'{word}': {count}번")
# 4. 최종 평가
if positive_count > negative_count:
print(f"\n🎉 결론: 이 영화는 대체로 좋은 평가를 받고 있습니다!")
elif negative_count > positive_count:
print(f"\n😔 결론: 이 영화는 대체로 아쉬운 평가를 받고 있습니다.")
else:
print(f"\n😐 결론: 이 영화에 대한 평가가 엇갈리고 있습니다.")
🌟 이번 챕터에서 배운 것
문자열 고급 메서드
.split()
: 문자열 분리.join()
: 문자열 합치기.replace()
: 문자열 바꾸기.strip()
: 공백 제거.isdigit()
,.isalpha()
: 문자열 타입 확인
리스트 고급 메서드
.sort()
: 정렬하기.remove()
: 값으로 제거.pop()
: 인덱스로 제거.index()
: 값의 위치 찾기.count()
: 개수 세기
실전 활용 기술
- 텍스트 분석: 단어 빈도, 감성 분석
- CSV 데이터 처리: 구조화된 데이터 다루기
- 데이터 정제: 불필요한 문자 제거, 형식 통일
💡 핵심 포인트:
- 문자열과 리스트 메서드를 조합하면 강력한 데이터 처리 가능
- 실제 데이터는 항상 정제 과정이 필요
- 단계별 처리로 복잡한 문제도 해결 가능
🚀 다음 챕터 예고: Chapter 5.3에서는 내장 함수들로 더욱 효율적인 코딩을 배워보겠습니다!