[英]Why split and readlines break for loop?
In our Python course, we were given this problem.在我们的 Python 课程中,我们遇到了这个问题。 We need to get accounts data from a file, perform some manipulations, and output the results... I've tried to use the first value of the account as an initial minimum.我们需要从文件中获取帐户数据,执行一些操作,然后 output 得到结果……我尝试使用帐户的第一个值作为初始最小值。 It's not working.它不工作。 It does something I have no idea and just shuts off entire for
loop.它做了一些我不知道的事情,只是关闭了整个for
循环。
accounts.txt
is a file that stores ID, savings type, and value. accounts.txt
是一个存储 ID、储蓄类型和价值的文件。 For example,例如,
66096 d 873323
29615 c 849977
45387 s 186640
95384 c 933363
13615 c 733321
46396 c 714610
17428 s 833671
43730 d 308296
92061 c 670423
51798 c 156063
58083 s 940510
So, here's the code:所以,这里是代码:
file=open('accounts.txt',mode='r')
linep=file.readlines()
mins=linep[0].split()[2]
sumc=0
maxc=0
sums=0
sumd=0
avrd=0
cntr=0
for i in file:
if i.split()[1]=='c':
sumc+=int(i.split()[2])
if int(i.split()[2])>=maxc:
maxc=int(i.split()[2])
elif i.split()[1]=='s':
sums+=int(i.split()[2])
if int(i.split()[2])<=mins:
mins=int(i.split()[2])
elif i.split()[1]=='d':
sumd+=int(i.split()[2])
cntr+=1
avrd=sums/cntr
print('The sum of all checking accounts is:',sumc)
print('The sum of all saving accounts is:',sums)
print('The sum of all deposit accounts is:',sumd)
print('The maximum balance for all checking accounts is:',maxc)
print('The minimum balance for all savings accounts is:',mins)
print('The average balance for all deposit accounts is:',avrd)
It works when you set the mins=99999999999
, but I don't think that's a good solution.当您设置mins=99999999999
时它可以工作,但我认为这不是一个好的解决方案。
Well, the output should've been like this:那么,output 应该是这样的:
The sum of all checking accounts is: 209315463
The sum of all saving accounts is: 50272914
The sum of all deposit accounts is: 19976046
The maximum balance for all checking accounts is: 998992
The minimum balance for all savings accounts is: 150
The average balance for all deposit accounts is: 554890.1666666666
But 2nd & 3rd lines screw everything up, so I have this但是第二行和第三行把一切都搞砸了,所以我有这个
The sum of all checking accounts is: 0
The sum of all saving accounts is: 0
The sum of all deposit accounts is: 0
The maximum balance for all checking accounts is: 0
The minimum balance for all savings accounts is: 873323
So, what do you think?所以你怎么看? I know, you can use min and max, but that's college we are talking about.我知道,你可以使用最小值和最大值,但那是我们正在谈论的大学。 They want to do it this way.他们想这样做。
Minimal changes to correct your code更正代码的最小更改
file=open('accounts.txt',mode='r')
linep=file.readlines()
mins=int(linep[0].split()[2]) # Need to make int
sumc=0
maxc=0
sums=0
sumd=0
avrd=0
cntr=0
for i in linep: # need to loop over linep not file
if i.split()[1]=='c':
sumc+=int(i.split()[2])
if int(i.split()[2])>=maxc:
maxc=int(i.split()[2])
elif i.split()[1]=='s':
sums+=int(i.split()[2])
if int(i.split()[2])<=mins:
mins=int(i.split()[2])
elif i.split()[1]=='d':
sumd+=int(i.split()[2])
cntr+=1
avrd=sums/cntr
print('The sum of all checking accounts is:',sumc)
print('The sum of all saving accounts is:',sums)
print('The sum of all deposit accounts is:',sumd)
print('The maximum balance for all checking accounts is:',maxc)
print('The minimum balance for all savings accounts is:',mins)
print('The average balance for all deposit accounts is:',avrd)
Code Refactoring (simpler and more efficient)代码重构(更简单、更高效)
file=open('accounts.txt',mode='r')
linep=file.readlines()
data = [x.split() for x in linep]
checking = [int(x[2]) for x in data if x[1] == 'c']
savings = [int(x[2]) for x in data if x[1] == 's']
deposits = [int(x[2]) for x in data if x[1] == 'd']
sumc, sums, sumd = sum(checking), sum(savings), sum(deposits)
maxc, mins, avrd = max(checking), min(savings), sum(deposits)/len(deposits)
print('The sum of all checking accounts is:',sumc)
print('The sum of all saving accounts is:',sums)
print('The sum of all deposit accounts is:',sumd)
print('The maximum balance for all checking accounts is:',maxc)
print('The minimum balance for all savings accounts is:',mins)
print('The average balance for all deposit accounts is:',avrd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.