我正在写一个函数来购买每年的软件许可证&返回每个月花费的金额。所以投入。功能之外的数组数据更改
#[company id, user id, purchase amount, date in yy-mm format, day when customer first registered]
['55463', '114769', $200, '14-5', '2/14/2013']
将返回
['55463', '114769', $17.5, '14-5', '2/14/2013']
['55463', '114769', $17.5, '14-6', '2/14/2013']
['55463', '114769', $17.5, '14-7', '2/14/2013']
['55463', '114769', $17.5, '14-8', '2/14/2013']
等我有打印出这是所期望的功能。但是,当我尝试访问函数外部的数据时,我突然写入的数组突然显示上个月重复了很多次。所以最后一个例子会是。
['55463', '114769', $17.5, '14-8', '2/14/2013']
['55463', '114769', $17.5, '14-8', '2/14/2013']
['55463', '114769', $17.5, '14-8', '2/14/2013']
['55463', '114769', $17.5, '14-8', '2/14/2013']
我不知道这一点,我的生活&将非常感谢任何帮助。这是我的代码。我相信这个错误是在范围(11)中的x开始的for循环中的某处。感谢您的帮助!
line_items = []
annualized = []
def csv_dict_reader(file_obj):
global board_info
reader = csv.DictReader(file_obj, delimiter=',')
for line in reader:
item = []
item.append(line["company id"])
item.append(line["user id"])
item.append(line["Amount"])
item.append(line["Transaction Date"])
item.append(line["FIrst Transaction"])
line_items.append(item)
if __name__ == "__main__":
with open("ChurnTest.csv") as f_obj:
csv_dict_reader(f_obj)
def dateSplit():
for i in range(1,4192):
line = line_items[i]
amount = line_items[i][2]
if len(amount) > 1 and line_items[i][1] == '114769':
if float(amount) > 150:
line_items[i][2] = float(amount)/12
#monthly_transactions.append(line_items[i])
year = int(line[3][:2])
month = int(line[3][4:])
for x in range(11):
month = int(month)
year = int(year)
newLine = line_items[i]
if month > 11:
year = year + 1
month = 1
else:
month = month + 1
newLine[3] = str(year) + "-" + str(month)
annualized.append(newLine)
#This prints out the data as I want it to be
print(annualized[len(annualized)-1])
else:
monthly_transactions.append(line_items[i])
dateSplit()
#This prints out the data incorrectly.
print(annualized)
请使用正确的缩进。这对Python非常重要。 – merlin2011
另外,你可以减少这个[最小示例](http://stackoverflow.com/help/mcve),并提供我们可以实际运行的数据样本? – jonrsharpe
专业提示:'年化[-1]'相当于'年化[len(年率化)-1]' – daveydave400