有没有办法可以将这段代码写得更紧凑?

Is there a way I can write this code more compact?

有没有办法让这段代码更短? 我是一个完全的初学者,我的任务是从账单列表中读出账单数量最多的公司。 我的想法是将每家公司都应用到一个列表中,计算该公司出现在该列表中的频率并将该数量附加到另一个列表中。 然后,我找出票据数量最多的公司的索引,将这些公司附加到新列表中,然后从该列表中删除所有重复项,以便票据最多的公司在列表中只出现一次。

谢谢!

def company_report(bills):
    """puts every company in list, counts how often each company is in list,
    reads the index of company which is the most frequent,
    uses index to read out name of company and how many bills it has."""
    # append all company names from each bill to list
    companies_bills = []
    for bill in bills:
        # use lower() to make sure that there is no uppercase lowercase error
        companies_bills.append(bill[0].lower())
    # count how many times each company is in company list
    dummy = []
    for i in companies_bills:
        dummy.append(companies_bills.count(i))
    # give out indicis of companies with most occurrences
    max_value = max(dummy)
    indices = [index for index, value in enumerate(dummy) if value == max_value]
    comp = []
    # use indicis to append companies with most occurrences to list
    for i in indices:
        comp.append(companies_bills[i])
    # remove duplicates from lists
    final_list = []
    for company in comp:
        if company not in final_list:
            final_list.append(company)
    row = "| {:17} | {:5} |"
    print("\n" + "{:=^29}".format(" Most Popular Company "))
    print(row.format("Company", "Bills"))
    print(row.format("-" * 17, "-" * 5))
    for company in final_list:
        print(row.format(company.title(), max(dummy)))
    print("=" * 29)
    return final_list

我没试过,但应该可以...

def company_report(账单): """将每家公司放入列表,计算每家公司出现在列表中的频率, 读取最频繁的公司索引, 使用索引读出公司名称及其有多少张账单。"""

# append all company names from each bill to list
companies_bills = [bill.lower() for bill in bills]
# count how many times each company is in company list
dummy = [companies_bills.count(i) for i in companies_bills]

# give out indicis of companies with most occurrences
max_value = max(dummy)

# use indicis to append companies with most occurrences to list
final_list = list(set([companies_bills[i] for i in [index for index, value in enumerate(dummy) if value == max_value]]))

row = "| {:17} | {:5} |"
print("\n" + "{:=^29}".format(" Most Popular Company "))
print(row.format("Company", "Bills"))
print(row.format("-" * 17, "-" * 5))
for company in final_list:
    print(row.format(company.title(), max_value))
print("=" * 29)
return final_list