获取循环内的下一个值 [英] Getting the next value within for loop
问题描述
我正在用Python编写一个程序,它从文件中读取银行数据,并将其存储在数据结构中,以备日后输出。
我有一个列表存储诸如
D,520,W,20,D,100
其中信是交易类型(取款或存款),数字是金额。
基本上我想要做的是:
我有一个for循环来计算余额,但是我无法进入下一个元素。如果项目=='D':
balance = balance + int(NEXT_ITEM),那么
$ b pre $ )
if item =='W':
balance = balance - int(NEXT_ITEM)
感谢您的帮助
data ='D,520,W ,20,D,100'.split(',')
def pair(lst):
it iter(lst)
return zip(it,it)
b alance = 0
for trans,amt成对(数据):
if trans =='D':
balance + = int(amt)
else:
balance - = int(amt)
print(balance)
I am writing a program in Python that reads in bank data from a file and stores it in data structures for output at a later time.
I have a list that stores the transactions like
D,520,W,20,D,100
Where the letter is the transaction type (Withdrawl or Deposit) and the numbers are the amount.
I have a for loop that will calculate the balance, but I am having trouble getting to the next element.
Essentially what I want to do is:
for item in theList:
if item == 'D':
balance = balance + int(NEXT_ITEM)
if item == 'W':
balance = balance - int(NEXT_ITEM)
Thanks for the help
data = 'D,520,W,20,D,100'.split(',')
def pairs(lst):
it = iter(lst)
return zip(it, it)
balance = 0
for trans,amt in pairs(data):
if trans == 'D':
balance += int(amt)
else:
balance -= int(amt)
print(balance)
这篇关于获取循环内的下一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!