有没有比这更好的方法来在列表理解中添加某些单词的例外?

问题描述 投票:0回答:1

我正在制作一个基本的图书馆管理系统,我想为某些介词词添加例外,条件是用户可以以大写、小写或作为标题输入例外词,即第一个字母是大写。

我已经制作了一个包含此类例外情况的单独列表,并且代码运行良好,但我想知道是否有更好的方法来做到这一点

class Library:
    def __init__(self, list_of_books):
        self.available_books = list_of_books

    def display_available_books(self):
        print()
        print("Available Books:")
        for books in self.available_books:
            print(books)

    def lend_book(self, requested_book):
        if requested_book in self.available_books:
            print(f"You have borrowed {requested_book}")
            self.available_books.remove(requested_book)
        else:
            print("Sorry, the book is not available")

    def add_book(self, returned_book):
        if returned_book not in self.available_books:  # This is with the assumption that only one copy of a book can
            # be present in the library at a time
            self.available_books.append(returned_book)
            print("You have returned the book. Thank you")
        else:
            print("The book is already in the library")


class Customer:
    def __init__(self):
        self.book = None

    def request_book(self):
        print("Enter the name of the book that you would like to borrow:")
        self.book = input()
        exception = ["and", "or", "at", "in", "for", "AND", "OR", "AT", "IN", "FOR", "And", "Or", "At", "In", "For"]
        self.book = " ".join([i.lower() if i in exception else i.capitalize() for i in self.book.
                             split()])
        print(self.book)
        return self.book

    def return_book(self):
        print("Enter the name of the book you want to return")
        self.book = input()
        exception = ["and", "or", "at", "in", "for", "AND", "OR", "AT", "IN", "FOR", "And", "Or", "At", "In", "For"]
        self.book = " ".join([i.lower() if i in exception else i.capitalize() for i in self.book.
                             split()])
        print(self.book)
        return self.book


library = Library(["Think and Grow Rich", "The Monk Who Sold His Ferrari", "Deep Work"])
customer = Customer()

while True:
    print("Enter 1 to display the available books")
    print("Enter 2 to request for a book")
    print("Enter 3 to return a book")
    print("Enter 4 to exit")
    user_choice = int(input())
    if user_choice == 1:
        library.display_available_books()
    elif user_choice == 2:
        book = customer.request_book()
        library.lend_book(book)
    elif user_choice == 3:
        book = customer.return_book()
        library.add_book(book)
    elif user_choice == 4:
        exit("Thank you!")
    else:
        print("Enter a valid input")

这是具体代码:

exception = ["and", "or", "at", "in", "for", "AND", "OR", "AT", "IN", "FOR", "And", "Or", "At", "In", "For"]
self.book = " ".join([i.lower() if i in exception else i.capitalize() for i in self.book.split()])

谢谢你

python list-comprehension
1个回答
0
投票
  1. 对于您的例外情况,您应该使用集合,检查集合是否包含某些项目比列表要快得多 检查列表中是否存在值的最快方法

  2. exceptions
    成为班级的一部分会更快一些

  3. 我认为你可以处理得更优雅一点,如果你想对例外中未包含的每个单词使用

    capitalize
    (这也避免了添加所有其他例外,如“aNd”等)

  4. 一般来说,您使用了最快的方法。所有其他选项(例如正则表达式和替换的组合)效率都低得多

     class Customer:
         def __init__(self):
             self.book = None
             self.exception = set(["and", "or", "at", "in", "for"])
    
         def request_book(self):
             print("Enter the name of the book that you would like to borrow:")
             book = input()
             self.book = " ".join(
                 [w.capitalize() if w not in self.exception else w
                  for w in book.lower().split()])
             print(self.book)
             return self.book
    
© www.soinside.com 2019 - 2024. All rights reserved.