我如何使用这种类型的函数来总结 3 天的销售交易并计算 3 天的销售交易并在我的主要逻辑中使用它
Posted
技术标签:
【中文标题】我如何使用这种类型的函数来总结 3 天的销售交易并计算 3 天的销售交易并在我的主要逻辑中使用它【英文标题】:How can I use this type of function that sums 3-day sales transactions and counting of 3-day sales transactions and use it in my main logic 【发布时间】:2022-01-05 11:58:56 【问题描述】:我知道这应该很简单,但我是编程新手,需要一些帮助来解决这个问题,谢谢
def daycalc(value, count):
for row in my_reader:
cnt = 0
if row[21] == 'Friday' or row[21] == 'Saturday' or row[21] == 'Sunday':
tot = tot + float(row[11])
cnt += 1
return tot, cnt
with open('POS.csv') as csvfile:
my_reader = csv.reader(csvfile, delimiter=',')
fricnt = 0
satcnt = 0
suncnt = 0
sun = 0
door = 0
hdr = []
sales = 0
frisales = 0
satsales = 0
sunsales = 0
total = 0
totcnt = 0
for row in my_reader:
if door == 0:
hdr.append(row)
door = 1
elif row[21] == 'Friday':
frisales = frisales + float(row[11])
fricnt += 1
elif row[21] == 'Saturday':
satsales = satsales + float(row[11])
satcnt += 1
elif row[21] == 'Sunday':
sunsales = sunsales + float(row[11])
suncnt += 1
total = frisales + satsales + sunsales
totcnt = fricnt + satcnt + suncnt
print('3-day Total Sales:',#sum of sales would be here , '3-day Average Sale:', #count would be here)
我尝试过以不同的方式重新创建函数,但我似乎可以将它应用到我的逻辑中并让它工作
【问题讨论】:
请出示 CSV 文件的样本 【参考方案1】:如果我们假设您的 CSV 文件如下所示:
0,0,0,0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,0,0,Saturday,0
0,0,0,0,0,0,0,0,0,0,0,12.5,0,0,0,0,0,0,0,0,0,Saturday,0
0,0,0,0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,0,0,Friday,0
0,0,0,0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,0,0,Saturday,0
0,0,0,0,0,0,0,0,0,0,0,50,0,0,0,0,0,0,0,0,0,Sunday,0
然后你可以这样处理它:
from collections import defaultdict
import csv
FILENAME = 'POS.csv'
D = defaultdict(lambda: [0,0.0])
DAYS = ['Friday', 'Saturday', 'Sunday']
def show(p, t, c):
print(f'p total=t:.2f count=c average=t/c:.2f')
with open(FILENAME) as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
k = row[21]
D[k][0] += 1
D[k][1] += float(row[11])
for day in DAYS:
if (count := D[day][0]) > 0:
show(day, D[day][1], count)
total = sum([D[k][1] for k in DAYS])
count = sum([D[k][0] for k in DAYS])
show('Three day', total, count)
【讨论】:
以上是关于我如何使用这种类型的函数来总结 3 天的销售交易并计算 3 天的销售交易并在我的主要逻辑中使用它的主要内容,如果未能解决你的问题,请参考以下文章