Bài tập Enumerate và Zip - Cơ bản
Phần 1: enumerate()
- Viết chương trình in list fruits với index từ 0.
fruits = ["apple", "banana", "cherry", "date"]
# Output:
# 0: apple
# 1: banana
# 2: cherry
# 3: date
# Code của bạn ở đây- Viết chương trình in list với số thứ tự bắt đầu từ 1.
fruits = ["apple", "banana", "cherry"]
# Output:
# 1: apple
# 2: banana
# 3: cherry
# Code của bạn ở đây- Viết hàm
find_indextìm vị trí đầu tiên của phần tử trong list.
def find_index(lst, target):
# Dùng enumerate()
pass
# Test
numbers = [10, 20, 30, 40, 50]
print(find_index(numbers, 30)) # 2
print(find_index(numbers, 100)) # -1 (không tìm thấy)- Viết hàm
find_all_indicestìm tất cả vị trí của phần tử.
def find_all_indices(lst, target):
# Dùng enumerate()
pass
# Test
numbers = [1, 2, 3, 2, 4, 2, 5]
print(find_all_indices(numbers, 2)) # [1, 3, 5]- Viết chương trình in menu với số thứ tự.
menu = ["Pizza", "Burger", "Pasta", "Salad"]
# Output:
# === MENU ===
# 1. Pizza
# 2. Burger
# 3. Pasta
# 4. Salad
# Code của bạn ở đây- Viết hàm
enumerate_dicttạo dictionary với index làm key.
def enumerate_dict(lst):
# Trả về {0: item1, 1: item2, ...}
pass
# Test
fruits = ["apple", "banana", "cherry"]
print(enumerate_dict(fruits))
# {0: 'apple', 1: 'banana', 2: 'cherry'}- Viết hàm
print_with_line_numbersin text với số dòng.
def print_with_line_numbers(text):
lines = text.split("\\n")
# In mỗi dòng với số thứ tự
pass
# Test
text = "Line one\\nLine two\\nLine three"
print_with_line_numbers(text)
# 1: Line one
# 2: Line two
# 3: Line three- Viết hàm
find_even_indicestìm vị trí của tất cả số chẵn.
def find_even_indices(numbers):
# Trả về list indices
pass
# Test
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
print(find_even_indices(numbers)) # [1, 3, 5, 7]- Viết hàm
replace_by_indexthay thế phần tử tại vị trí cụ thể.
def replace_by_index(lst, target_index, new_value):
# Dùng enumerate()
pass
# Test
fruits = ["apple", "banana", "cherry"]
print(replace_by_index(fruits, 1, "orange"))
# ["apple", "orange", "cherry"]- Viết hàm
enumerate_stringđánh số từng ký tự trong chuỗi.
def enumerate_string(text):
# In index: char
pass
# Test
enumerate_string("Python")
# 0: P
# 1: y
# 2: t
# 3: h
# 4: o
# 5: nPhần 2: zip()
- Viết chương trình kết hợp 2 lists names và ages.
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
# Output:
# Alice: 25
# Bob: 30
# Charlie: 35
# Code của bạn ở đây- Viết hàm
create_dict_from_liststạo dictionary từ 2 lists.
def create_dict_from_lists(keys, values):
# Dùng zip() và dict()
pass
# Test
keys = ["name", "age", "city"]
values = ["Alice", 25, "Hanoi"]
print(create_dict_from_lists(keys, values))
# {'name': 'Alice', 'age': 25, 'city': 'Hanoi'}- Viết hàm
combine_listskết hợp 3 lists thành list of tuples.
def combine_lists(list1, list2, list3):
# Dùng zip()
pass
# Test
names = ["Alice", "Bob"]
ages = [25, 30]
cities = ["Hanoi", "HCMC"]
print(combine_lists(names, ages, cities))
# [('Alice', 25, 'Hanoi'), ('Bob', 30, 'HCMC')]- Viết hàm
sum_paralleltính tổng từng cặp phần tử.
def sum_parallel(list1, list2):
# Trả về list tổng
pass
# Test
list1 = [1, 2, 3, 4]
list2 = [10, 20, 30, 40]
print(sum_parallel(list1, list2))
# [11, 22, 33, 44]- Viết hàm
compare_listsso sánh từng cặp phần tử.
def compare_lists(list1, list2):
# In ra các cặp bằng nhau và khác nhau
pass
# Test
list1 = [1, 2, 3, 4]
list2 = [1, 5, 3, 8]
compare_lists(list1, list2)
# Position 0: 1 == 1 (Equal)
# Position 1: 2 != 5 (Different)
# Position 2: 3 == 3 (Equal)
# Position 3: 4 != 8 (Different)- Viết hàm
unzip_pairstách list of pairs thành 2 lists.
def unzip_pairs(pairs):
# Dùng zip(*pairs)
pass
# Test
pairs = [(1, 'a'), (2, 'b'), (3, 'c')]
nums, letters = unzip_pairs(pairs)
print(nums) # [1, 2, 3]
print(letters) # ['a', 'b', 'c']- Viết hàm
calculate_total_scorestính tổng điểm 3 môn.
def calculate_total_scores(math, english, science):
# Trả về list tổng điểm
pass
# Test
math = [85, 90, 78]
english = [88, 82, 95]
science = [92, 88, 80]
print(calculate_total_scores(math, english, science))
# [265, 260, 253]- Viết hàm
format_full_nameskết hợp first names và last names.
def format_full_names(first_names, last_names):
# Trả về list full names
pass
# Test
first = ["Alice", "Bob", "Charlie"]
last = ["Smith", "Johnson", "Brown"]
print(format_full_names(first, last))
# ['Alice Smith', 'Bob Johnson', 'Charlie Brown']- Viết hàm
multiply_parallelnhân từng cặp phần tử.
def multiply_parallel(list1, list2):
# Trả về list tích
pass
# Test
list1 = [2, 3, 4, 5]
list2 = [10, 20, 30, 40]
print(multiply_parallel(list1, list2))
# [20, 60, 120, 200]- Viết hàm
create_person_recordstạo list of dictionaries.
def create_person_records(names, ages, cities):
# Mỗi person là dict với keys: name, age, city
pass
# Test
names = ["Alice", "Bob"]
ages = [25, 30]
cities = ["Hanoi", "HCMC"]
print(create_person_records(names, ages, cities))
# [
# {'name': 'Alice', 'age': 25, 'city': 'Hanoi'},
# {'name': 'Bob', 'age': 30, 'city': 'HCMC'}
# ]