Python新手求助:统计列表名称数及球队夺冠次数并写入文件
Hey there! Let's work through your two Python problems step by step—since you're new to the language, I'll keep things clear and practical.
Problem 1: Get the count of names in a list and write to a file
First, let's cover two common scenarios you might mean: total number of names (including duplicates) or the number of unique names. Here's how to handle both:
Example Code
# Sample list of names name_list = ["Alice", "Bob", "Alice", "Charlie", "Bob", "Alice"] # 1. Total number of names (including duplicates) total_count = len(name_list) # 2. Number of unique names unique_count = len(set(name_list)) # set() automatically removes duplicates # Write results to a file (use `with` to auto-close the file safely) with open("name_counts.txt", "w") as f: f.write(f"Total number of names: {total_count}\n") f.write(f"Number of unique names: {unique_count}\n")
Key Notes
len()gives the total length of the list instantly—super straightforward.set(name_list)converts your list to a collection of unique values, thenlen()counts those unique entries.- Using
with open(...)is Python's best practice for file handling—it ensures the file closes properly even if an error pops up.
Problem 2: Count championship wins per team and write to file
Your nested list data (like 1991 Minnesota, 1992 Toronto) likely falls into one of two formats: either a list of lists ([[1991, "Minnesota"], [1992, "Toronto"]]) or a list of strings (["1991 Minnesota", "1992 Toronto"]). I'll cover both cases, plus finish your write_tab_separated function (quick note: you had a small typo—seperated should be separated).
We'll use collections.Counter—a built-in Python tool made exactly for counting item occurrences.
Case 1: Nested list of [year, team]
from collections import Counter def write_tab_separated(filename): # Replace this with your actual championship data championship_data = [[1991, "Minnesota"], [1992, "Toronto"], [1993, "Toronto"]] # Extract all team names from the nested list team_names = [entry[1] for entry in championship_data] # Count how many times each team won win_counts = Counter(team_names) # Write results to file in "Team Name Count" format with open(filename, "w") as file: for team, count in win_counts.items(): file.write(f"{team} {count}\n") # Call the function to write to a file write_tab_separated("championship_wins.txt")
Case 2: List of strings like "1991 Minnesota"
If your data is stored as strings instead of nested lists, just split each string to grab the team name:
from collections import Counter def write_tab_separated(filename): championship_data = ["1991 Minnesota", "1992 Toronto", "1993 Toronto"] # Split each string and take the second part (the team name) team_names = [entry.split()[1] for entry in championship_data] win_counts = Counter(team_names) with open(filename, "w") as file: for team, count in win_counts.items(): file.write(f"{team} {count}\n") write_tab_separated("championship_wins.txt")
Key Notes
Counter(team_names)automatically tallies up how many times each team appears—no manual counting needed.- The list comprehension
[entry[1] for entry in ...]is a concise way to pull just the team names out of your raw data. - Sticking with
with open(...)keeps your file handling safe and avoids accidental resource leaks.
内容的提问来源于stack exchange,提问作者S Peter

