Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git exercise #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions exercise_1/exercise_1_LongBui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Các thao tác trong file này đều có thể được thực hiện dễ dàng bằng thư viện pandas. Tuy nhiên
# để các bạn làm quen hơn với Python, hãy cố gắng sử dụng những lệnh căn bản nhất của Python. Từ đó
# hi vọng các bạn thấy được các thư viện như Pandas, Numpy giúp chúng ta tiết kiệm thời gian cho
# những thao tác quen thuộc này thế nào

# Trước tiên chúng ta hãy học đọc nội dung trong file .csv bằng Python. Trong Pandas việc này rất
# đơn giản, chỉ cần dùng pandas.read_csv("filename") là ta đã nhập dữ liệu trong filename.csv vào
# một dataframe. Tuy nhiên nếu chỉ sử dụng Python thuần tuý, ta cần đến module csv.
import csv

# Sau đây là các mở file Hanoi.csv bằng Python. Ta sẽ lưu dữ liệu của file này vào list
# monthly_weather
monthly_weather = []
with open('Hanoi.csv', 'r') as f:
reader = csv.DictReader(f)
for row in reader:
monthly_weather.append(row)

# Checkpoint 1: Hãy in monthly_weather ra và nhận xét về loại dữ liệu của các thành phần bên trong.
# (Tham khảo qua định nghĩa của OrderedDict tại
# https://docs.python.org/3/library/collections.html#collections.OrderedDict. Do OrderedDict cũng
# thuộc class Dict, mọi hàm của Dict đều có thể dùng được cho OrderDict.
# TODO

# Comment: monthly_weather is an OrderedDict which includes 12 Dicts, each row is a Dict.
# The order is decided by key "month"


# Dùng for loop, ta có thể in ra từng row trong monthly_weather. (Hãy uncomment những dòng sau)
for month in monthly_weather:
print(month)


# Checkpoint 2: Dùng for loop, với mỗi row trong monthly_weather, in ra các keys của row
# TODO

for month in monthly_weather:
for item in month:
print(item)

# Checkpoint 3: Tìm tháng có số ngày mưa (rainy days) lớn nhất trong năm.
# Các bạn chú ý đối chiếu lại với dữ liệu trong file csv ban đầu. Nếu cần, hàm int(string) sẽ giúp
# lấy giá trị số của một xâu.
# TODO

Maximum_rainy_days = -1; day_number = -1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tách làm 2 dòng em nhé. Với cả đừng đặt tên biến viết hoa chữ đầu như vậy, ("maximum_rainy_days").


for month in monthly_weather:
if (Maximum_rainy_days < int(month['rainy_days'])):
Maximum_rainy_days = int(month['rainy_days'])
day_number = month['month']

print('Maximum_rainy_days month = ', day_number)
print('number of days', Maximum_rainy_days)