应用范围
在费用计算的时候会有很多核销的情况,不是一对多的,而是多对多的。例如付款和发票,收款和发票金额会有不一致的情况。
例如:三张付款单(单号,金额) 总数300 : (FK01,100) (FK02, 100) (FK03, 100)
两张发票(单号,金额) 总数300 : (FP01, 100),(FP02, 200)
示例代码
# -*- coding: UTF-8 -*-
aa = [
{
'id': 1,
'num': 5
},
{
'id': 2,
'num': 7
}
]
bb = [
{
'id': 'a',
'num': 1
},
{
'id': 'b',
'num': 6
},
{
'id': 'c',
'num': 5
}
]
cc = []
def geta ():
if len(aa) == 0:
return {}
else:
return aa.pop(0)
def getb ():
if len(bb) == 0:
return {}
else:
return bb.pop(0)
a = {}
b = {}
# 判断是否为空
aEnd = False
bEnd = False
# 判断是否取下一个值
aNext = True
bNext = True
# 存储中间值
aTemp = {}
bTemp = {}
# 只有都为空停止
while (not aEnd) or (not bEnd):
if aNext:
a = geta()
print(f'--取值a: {a}')
if a == {}:
aEnd = True
else:
a = aTemp
print(f'赋值a: {a}')
if bNext:
b = getb()
print(f'--取值b: {b}')
if b == {}:
bEnd = True
else:
b = bTemp
print(f'赋值b: {b}')
# a或b,原始列表和临时列表都为空时停止
if (aEnd == True and a == {}) or (bEnd == True and b == {}):
break
print(a, b)
if a['num'] > b['num']:
aNext = False
bNext = True
aTemp = {
'id': a['id'],
'num': a['num'] - b['num']
}
bTemp = {}
cc.append({
'aId': a['id'],
'num': b['num'],
'bId': b['id']
})
elif a['num'] < b['num']:
aNext = True
bNext = False
aTemp = {}
bTemp = {
'id': b['id'],
'num': b['num'] - a['num']
}
cc.append({
'aId': a['id'],
'num': a['num'],
'bId': b['id']
})
else:
aNext = True
bNext = True
aTemp ={}
bTemp ={}
cc.append({
'aId': a['id'],
'num': a['num'],
'bId': b['id']
})
for c in cc:
print(f'核销记录{c}')