History
home
BDA 연혁
home

- 시퀀스 자료형과 Index

시퀀스 자료형은 무엇인가?
시퀀스 자료형의 개념 이해
시퀀스 자료형이 중요한 이유
시퀀스 자료들의 특징
인덱스 Index
인덱스에 대한 개념 이해
인덱스의 구조와 Python 원리 이해
Python에서 인덱스를 통해 할 수 있는 것들?
코드를 통한 다양한 응용 예시
인덱스를 통한 슬라이싱
인덱스를 통한 슬라이싱 이해
다양한 코드 예시
수업 예시 코드
# 예시 코드 a = [1,2] # 시퀀스 객체의 순서를 본다고 하면 0,1 #전채 개수는 2개 b= [1,2,3,4,5] #0,1,2,3,4 # 전체 개수는 5개 c= [1,2,3,4,5,10] # 0,1,2,3,4,5 #전체 개수 6개 # 혼합 데이터 타입의 튜플 리스트 생성 mixed_data_list = [("apple", 3, True), ("banana", 5, False), ("orange", 2, True)] # 리스트 컴프리헨션을 사용하여 특정 정보 추출 fruit_names = [item[0] for item in mixed_data_list] total_quantity = sum(item[1] for item in mixed_data_list if item[2]) # 문자열의 튜플 생성 words_tuple = ("apple", "banana", "orange", "grape", "kiwi") # 튜플 언패킹과 리스트 컴프리헨션 사용 uppercase_words = [word.upper() for word in words_tuple] # 사용자 정의 구분자로 튜플에서 문자열 결합 combined_string = " | ".join(words_tuple) # 형식화된 값이 있는 문자열 생성 formatted_string = f"총 수량: {total_quantity}, 과일: {', '.join(fruit_names)}" # 문자열에 대한 세트 연산 사용 string1 = "apple" string2 = "orange" common_characters = set(string1) & set(string2) unique_characters = set(string1) | set(string2) # 고급 문자열 multiline_text = """여러 줄에 걸친 여러 줄 텍스트입니다 삼중 따옴표를 사용합니다.""" reversed_multiline = "\n".join(line[::-1] for line in multiline_text.split('\n')) # Index 예제 # 딕셔너리 목록 생성 people_data = [ {"name": "Alice", "age": 25, "city": "New York"}, {"name": "Bob", "age": 30, "city": "San Francisco"}, {"name": "Charlie", "age": 22, "city": "Los Angeles"}, ] # 리스트의 딕셔너리에서 값에 접근 first_names = [person["name"] for person in people_data] second_age = data_dict["ages"][1] # 중첩된 인덱싱을 사용하여 특정 값에 접근 city_of_charlie = people_data[2]["city"] last_city = data_dict["cities"][-1] # 범위의 요소를 추출하기 위해 슬라이싱 사용 selected_people = people_data[1:3] selected_ages = data_dict["ages"][::2] # enumerate를 사용하여 인덱스 및 값 얻기 for index, person in enumerate(people_data): print(f"Person {index + 1}: {person['name']}") # 인덱싱과 문자열 포맷팅을 결합 formatted_data = [f"{person['name']} ({person['age']}세)" for person in people_data] # 슬라이싱 예제 numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # 숫자 목록 생성 # 특정 하위 시퀀스 얻기 위해 슬라이싱 first_three = numbers[:3] last_three = numbers[-3:] middle_section = numbers[3:7] # 슬라이싱에서 단계 사용 even_numbers = numbers[1::2] reverse_odd_numbers = numbers[-1::-2] words = ["apple", "banana", "orange", "grape", "kiwi"] # 문자열 목록 생성 # 문자열을 슬라이싱하여 특정 부분 문자열 추출 substring1 = words[1][1:4] substring2 = words[2][::-1] # 인덱스 슬라이싱과 리스트 컴프리헨션 결합 filtered_words = [word[1:] for word in words if len(word) > 5] # zip 및 슬라이싱을 사용하여 행렬 전치 matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] transposed_matrix = [list(row) for row in zip(*matrix)]
Python
복사