如何迭代作为函数参数传递的列表?

2024-06-23 19:43:44 发布

您现在位置:Python中文网/ 问答频道 /正文

需要帮助了解在作为函数参数传递时,如何高效、正确地处理迭代列表。下面是一些示例代码:

from datetime import datetime, timedelta, timezone
from dateutil.relativedelta import relativedelta

items = [{'freq':'qtrly','date':'01/02/2019','amount':40,'quantity':5}, {'freq':'annly','date':'04/06/2018','amount':20,'quantity':4}]

def splitItems(Item, mmmm):
    print("splitItem() function was called")
    finalRows = []
    for itm in Item:
        print("Called from within 1st for-loop")
        print("Original List: ", itm)
        for e in range(mmmm):
            print("Called from within 2nd for-loop")
            new = itm.copy()
            new['date']  = datetime.strptime(itm['date'],'%m/%d/%Y') + relativedelta(months=+e)
            new['amount'] = itm['amount'] / mmmm
            new['quantity'] = itm['quantity'] / mmmm
            finalRows.append(new)
            print("New List: ", datetime.strftime(new['date'],'%m/%d/%Y'), new['amount'],new['quantity'])
    return finalRows

for i in items:
    freq = i['freq']
    final = items
    if freq == 'qtrly':
        print('frequency is: ',freq)
        final = splitItems(items,3)
    elif freq == 'annly':
        print('frequency is: ',freq)
        final = splitItems(items,12)
    elif freq == 'semi-annly':
        print('frequency is: ',freq)
        final = splitItems(items,6)
    else:
        None

当列表中只有一行/元组(即项)时,我会得到正确的结果。例如,如果只有一行的freq='qtry';然后该函数产生正确的输出,即:

frequency is:  qtrly
splitItem() function was called
Called from within 1st for-loop
Original List:  {'freq': 'qtrly', 'date': '01/02/2019', 'amount': 40, 'quantity': 5}
Called from within 2nd for-loop
New List:  01/02/2019 13.333333333333334 1.6666666666666667
Called from within 2nd for-loop
New List:  02/02/2019 13.333333333333334 1.6666666666666667
Called from within 2nd for-loop
New List:  03/02/2019 13.333333333333334 1.6666666666666667

但是,对于列表中超过1个元组/行/项,它首先对列表中的每个项“qtry”迭代3次,然后对相同的元组/行“annly”迭代12次,如下所示,这是不正确的:

frequency is:  qtrly
splitItem() function was called
Called from within 1st for-loop
Original List:  {'freq': 'qtrly', 'date': '01/02/2019', 'amount': 40, 'quantity': 5}
Called from within 2nd for-loop
New List:  01/02/2019 13.333333333333334 1.6666666666666667
Called from within 2nd for-loop
New List:  02/02/2019 13.333333333333334 1.6666666666666667
Called from within 2nd for-loop
New List:  03/02/2019 13.333333333333334 1.6666666666666667
Called from within 1st for-loop
Original List:  {'freq': 'annly', 'date': '04/06/2018', 'amount': 20, 'quantity': 4}
Called from within 2nd for-loop
New List:  04/06/2018 6.666666666666667 1.3333333333333333
Called from within 2nd for-loop
New List:  05/06/2018 6.666666666666667 1.3333333333333333
Called from within 2nd for-loop
New List:  06/06/2018 6.666666666666667 1.3333333333333333
frequency is:  annly
splitItem() function was called
Called from within 1st for-loop
Original List:  {'freq': 'qtrly', 'date': '01/02/2019', 'amount': 40, 'quantity': 5}
Called from within 2nd for-loop
New List:  01/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  02/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  03/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  04/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  05/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  06/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  07/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  08/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  09/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  10/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  11/02/2019 3.3333333333333335 0.4166666666666667
Called from within 2nd for-loop
New List:  12/02/2019 3.3333333333333335 0.4166666666666667
Called from within 1st for-loop
Original List:  {'freq': 'annly', 'date': '04/06/2018', 'amount': 20, 'quantity': 4}
Called from within 2nd for-loop
New List:  04/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  05/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  06/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  07/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  08/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  09/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  10/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  11/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  12/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  01/06/2019 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  02/06/2019 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  03/06/2019 1.6666666666666667 0.3333333333333333 

预期输出为:

frequency is:  qtrly
splitItem() function was called
Called from within 1st for-loop
Original List:  {'freq': 'qtrly', 'date': '01/02/2019', 'amount': 40, 'quantity': 5}
Called from within 2nd for-loop
New List:  01/02/2019 13.333333333333334 1.6666666666666667
Called from within 2nd for-loop
New List:  02/02/2019 13.333333333333334 1.6666666666666667
Called from within 2nd for-loop
New List:  03/02/2019 13.333333333333334 1.6666666666666667

frequency is:  annly
splitItem() function was called
Called from within 1st for-loop
Original List:  {'freq': 'annly', 'date': '04/06/2018', 'amount': 20, 'quantity': 4}
Called from within 2nd for-loop
New List:  04/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  05/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  06/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  07/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  08/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  09/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  10/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  11/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  12/06/2018 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  01/06/2019 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  02/06/2019 1.6666666666666667 0.3333333333333333
Called from within 2nd for-loop
New List:  03/06/2019 1.6666666666666667 0.3333333333333333

Tags: fromloopnewfordateamountquantitylist

热门问题