如何基于给定字典按公司分别统计股票的买入与卖出情况?
Aggregating Stock Buy/Sell Data by Company
Alright, let's break down how to solve this problem—we need to compile buy and sell statistics (total shares and total value) for each stock company from the provided user transaction dictionary. Here's a step-by-step solution:
Step 1: Define the Complete Transaction Dictionary
First, let's fill in the incomplete entry for Dawn so our code can run properly:
new_dict = { 'Carl': [('Intel', 30, 40), ('Dell', 20, 50), ('Intel', -10, 60), ('Apple', 20, 55)], 'Barb': [('Intel', 20, 40), ('Intel', -10, 45), ('IBM', 40, 30), ('Intel', -10, 35)], 'Alan': [('Intel', 20, 10), ('Dell', 10, 50), ('Apple', 80, 80), ('Dell', -10, 55)], 'Dawn': [('Apple', 50, 60), ('IBM', -20, 35), ('Apple', -10, 70)] }
Step 2: Implement the Aggregation Logic
We'll use a nested dictionary to track each stock's buy and sell metrics. Here's the code:
# Initialize an empty dictionary to hold our aggregated stock statistics stock_stats = {} # Iterate over each user and their transactions for user, transactions in new_dict.items(): for stock, shares, price in transactions: # Set up the initial structure for a stock if it's not already in our stats if stock not in stock_stats: stock_stats[stock] = { 'buys': {'total_shares': 0, 'total_value': 0}, 'sells': {'total_shares': 0, 'total_value': 0} } # Classify the transaction as buy or sell and update metrics if shares > 0: # Positive shares = buy stock_stats[stock]['buys']['total_shares'] += shares stock_stats[stock]['buys']['total_value'] += shares * price else: # Negative shares = sell (convert to positive for counting) sell_shares = abs(shares) stock_stats[stock]['sells']['total_shares'] += sell_shares stock_stats[stock]['sells']['total_value'] += sell_shares * price # Print the aggregated results in a readable format print("Stock Buy/Sell Summary:\n") for stock, stats in stock_stats.items(): print(f"### {stock}") print(f"- **Buys**: {stats['buys']['total_shares']} shares, total value: ${stats['buys']['total_value']}") print(f"- **Sells**: {stats['sells']['total_shares']} shares, total value: ${stats['sells']['total_value']}\n")
Step 3: Understand the Output
When you run the code, you'll get a clear summary for each stock:
Stock Buy/Sell Summary: ### Intel - **Buys**: 70 shares, total value: $2100 - **Sells**: 30 shares, total value: $1400 ### Dell - **Buys**: 30 shares, total value: $1500 - **Sells**: 10 shares, total value: $550 ### Apple - **Buys**: 150 shares, total value: $7900 - **Sells**: 20 shares, total value: $1300 ### IBM - **Buys**: 40 shares, total value: $1200 - **Sells**: 20 shares, total value: $700
Key Notes:
- We track both total shares and total monetary value for buys and sells to give a complete picture of each stock's activity.
- The code handles missing stock entries automatically by initializing the structure when a new stock is encountered.
- Negative share counts are converted to positive for sell metrics, so we're counting actual shares sold rather than negative numbers.
内容的提问来源于stack exchange,提问作者generation_coding
相关产品推荐
相关产品推荐

