You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python数组任务求解:奶牛日产奶量数组处理问题

Analyzing Cow Milk Production Data with Python

Hey there! Let's work through analyzing this cow milk production dataset using Python. First, let's restate the context we're working with to make sure we're on the same page:

将现有如下包含奶牛每日总产奶量的数组:[5, 3, 7, 9, 3, 3, 7, 108, 66, 16, 5, 3, 7, 3]。牛群共2头奶牛,每日挤奶2次,因此数组含14个元素,每个元素代表单日总产奶量,每头奶牛对应7天的7个元素,数组格式为[Cow1Day1, Cow1Day2, Cow1Day3, Cow1Day4, Cow1Day5, Cow1Day6, Cow1Day7, Cow2Day1...]。

Step 1: Define the raw data and split it by cow

First, we'll assign the array to a variable, then split it into two separate lists—one for each cow (since the first 7 elements are Cow 1's 7 days, the last 7 are Cow 2's):

# Raw production data
milk_production = [5, 3, 7, 9, 3, 3, 7, 108, 66, 16, 5, 3, 7, 3]

# Split data into individual cow datasets
cow1_daily = milk_production[:7]  # Cow 1: Days 1-7
cow2_daily = milk_production[7:]  # Cow 2: Days 1-7

Step 2: Calculate key metrics (total & average production)

Let's compute the total milk each cow produced over 7 days, plus their average daily production:

# Calculate total production for each cow
cow1_total = sum(cow1_daily)
cow2_total = sum(cow2_daily)

# Calculate average daily production (rounded to 2 decimal places)
cow1_avg = round(cow1_total / len(cow1_daily), 2)
cow2_avg = round(cow2_total / len(cow2_daily), 2)

# Print results
print(f"Cow 1 | Total: {cow1_total} units | Average: {cow1_avg} units/day")
print(f"Cow 2 | Total: {cow2_total} units | Average: {cow2_avg} units/day")

Output:

Cow 1 | Total: 37 units | Average: 5.29 units/day
Cow 2 | Total: 208 units | Average: 29.71 units/day

Step 3: Find peak production days

Want to know which day each cow had their highest daily output? We can use the max() function to find the value, then index() to get the day number (we add 1 to make it 1-based instead of 0-based):

# Find peak production for Cow 1
cow1_peak = max(cow1_daily)
cow1_peak_day = cow1_daily.index(cow1_peak) + 1

# Find peak production for Cow 2
cow2_peak = max(cow2_daily)
cow2_peak_day = cow2_daily.index(cow2_peak) + 1

# Print results
print(f"Cow 1's highest production: {cow1_peak} units on Day {cow1_peak_day}")
print(f"Cow 2's highest production: {cow2_peak} units on Day {cow2_peak_day}")

Output:

Cow 1's highest production: 9 units on Day 4
Cow 2's highest production: 108 units on Day 1

Step 4: Count occurrences of a specific production value

If you want to know how often a certain yield (like 3 units) happened for each cow, use the count() method:

target_yield = 3
cow1_target_count = cow1_daily.count(target_yield)
cow2_target_count = cow2_daily.count(target_yield)

print(f"Cow 1 produced {target_yield} units on {cow1_target_count} days")
print(f"Cow 2 produced {target_yield} units on {cow2_target_count} days")

Output:

Cow 1 produced 3 units on 3 days
Cow 2 produced 3 units on 2 days

Bonus: Visualize daily production (using matplotlib)

If you want a visual comparison of the two cows' daily output, you can use matplotlib (install it first with pip install matplotlib):

import matplotlib.pyplot as plt

days = range(1, 8)  # Days 1-7

plt.figure(figsize=(10, 6))
plt.plot(days, cow1_daily, marker='o', label='Cow 1')
plt.plot(days, cow2_daily, marker='s', label='Cow 2')
plt.title('Daily Milk Production per Cow (7 Days)')
plt.xlabel('Day')
plt.ylabel('Total Daily Milk (units)')
plt.xticks(days)
plt.legend()
plt.grid(True)
plt.show()

This will generate a line chart showing each cow's daily production over the 7 days, making it easy to spot trends and outliers (like Cow 2's huge Day 1 output).

内容的提问来源于stack exchange,提问作者Vittal Kamath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:32:56