我试图在我的程序中运行基本的统计学,而不使用numpy和pandas。我想计算一下借书的总平均数。然而,我的total_贷款len()没有返回被借的书的总数,因此导致我的程序返回1,这是不正确的。
任何建议将不胜感激!
我下面的代码应该返回一本书被借阅的总平均天数,如果它同时出现在bookloans.csv和books.csv中,基于公共的book_id字段(只有books.csv有标题,bookloans.csv没有任何标题)
import csv
total_loan_days = 0
total_loans = 0
# Open the books file and read the title and authors into a list
with open('books.csv', 'r', encoding='utf-8-sig') as csv_books_file:
books_reader = csv.reader(csv_books_file)
next(books_reader) # skip the header row
for row in books_reader:
books = row[0]
with open('bookloans.csv', 'r', encoding='utf-8-sig') as csv_bookloans_file:
loans_reader = csv.reader(csv_bookloans_file)
for rows in loans_reader:
book_number = row[0]
return_date = rows[3]
start_date = rows[2]
date_diff = int(return_date) - int(start_date)
total_loan_days += date_diff
total_loans = len(book_number)
#Calculate the overall average loan days for all books
if date_diff > 1:
overall_average_loan_days = total_loan_days / total_loans
else:
overall_average_loan_days = 1
print(f"Overall Average Loan Days: {over`all_average_loan_days}")
字符串
1条答案
按热度按时间zysjyyx41#
我首先将主图书列表读入一个dict,并键入图书的ID:
字符串
阅读此CSV后:
型
字典看起来像:
型
然后,我将贷款CSV读入一个单独的dict,并将 checkout 和返回的时间戳解析为真实的日期值(这将使数学和统计数据更容易,更准确):
型
阅读此贷款CSV后:
型
贷款dict看起来像:
型
一个图书ID,它指向一个日期对列表(每对日期都被检出并返回)。
现在,有了所有可用书籍的完整列表和每本书(实际上是借出的)的借阅日期列表,您可以应用您需要的任何逻辑/数学。
我去了:
型
它打印:
型