Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将多个值追加到一个列表中,同时通过另一个列表递增_Python_List - Fatal编程技术网

在Python中将多个值追加到一个列表中,同时通过另一个列表递增

在Python中将多个值追加到一个列表中,同时通过另一个列表递增,python,list,Python,List,其目标是定期为每个账户输入金额和日期。账户是静态的(但可以追加更多)。我试图做的是循环遍历每个帐户,并为每个帐户输入金额和日期。我在增量行中,可能还有附加行中,认为我做错了什么 之后,我想以一种有意义的方式将结果打印到屏幕上(我知道我的结果不正确,并且不会以合理的方式显示) 有什么想法吗?谢谢 account = ['401k', 'RothIRA', 'HSA'] amount = [] date = [] while True: print('Enter the amount fo

其目标是定期为每个账户输入金额和日期。账户是静态的(但可以追加更多)。我试图做的是循环遍历每个帐户,并为每个帐户输入金额和日期。我在增量行中,可能还有附加行中,认为我做错了什么

之后,我想以一种有意义的方式将结果打印到屏幕上(我知道我的结果不正确,并且不会以合理的方式显示)

有什么想法吗?谢谢

account = ['401k', 'RothIRA', 'HSA']
amount = []
date = []

while True:
    print('Enter the amount for your ' + account[0] + ' account')
    act = input()
    amount.append(act)
    print('Enter the date for this total')
    dt = input()
    date.append(dt)
    account[] += 1
    if act == '':
        break


print(account, amount, date)

在您的数据结构发生轻微变化后:

account={ '401K': { 'amount' : 0, 'date': 0 },
          'ROthIRA': { 'amount':0, 'date': 0},
          'HSA': { 'amount': 0, 'date': 0} }

for eachKey in account.keys:
    account[eachKey]['amount'] = input()
    account[eachKey]['date'] = input()


print account

在您的数据结构发生轻微变化后:

account={ '401K': { 'amount' : 0, 'date': 0 },
          'ROthIRA': { 'amount':0, 'date': 0},
          'HSA': { 'amount': 0, 'date': 0} }

for eachKey in account.keys:
    account[eachKey]['amount'] = input()
    account[eachKey]['date'] = input()


print account

我想这就是你想要做的:

i = 0

while i < len(account):
    print('Enter the amount for your ' + account[i] + ' account')
    act = input()
    amount.append(act)
    print('Enter the date for this total')
    dt = input()
    date.append(dt)
    i += 1

此外,您的所有列表(帐户、金额、日期)都通过索引链接。像其他人发布的那样使用字典要干净得多。

我想这就是你想要做的:

i = 0

while i < len(account):
    print('Enter the amount for your ' + account[i] + ' account')
    act = input()
    amount.append(act)
    print('Enter the date for this total')
    dt = input()
    date.append(dt)
    i += 1

此外,您的所有列表(帐户、金额、日期)都通过索引链接。像其他人发布的那样使用字典要干净得多。

你的解决方案就是要一本字典。我强烈建议您使用字典来制作此文档。无需
帐户[]+=1
。当您执行
append
操作时,列表大小会自动增加。@idjaw谢谢,我尝试了一本字典,但没有取得太大进展。如果您的最终解决方案真的这么简单,请仔细阅读如何使用。它们非常适合解决这类问题。如果你需要更多的功能,考虑为每个帐户定义你自己的对象<代码>类< /代码>,这样你就不需要同时计算了。这将使您能够更好地控制每个帐户。@ksai帐户[]+=1用于硬编码帐户,并在填充其他2个列表时循环使用它们。您的解决方案迫切需要一本字典。我强烈建议您使用字典来制作此文档。无需
帐户[]+=1
。当您执行
append
操作时,列表大小会自动增加。@idjaw谢谢,我尝试了一本字典,但没有取得太大进展。如果您的最终解决方案真的这么简单,请仔细阅读如何使用。它们非常适合解决这类问题。如果你需要更多的功能,考虑为每个帐户定义你自己的对象<代码>类< /代码>,这样你就不需要同时计算了。这将使您能够更好地控制每个帐户。@ksai帐户[]+=1用于硬编码帐户,并在填充其他两个列表时循环使用它们谢谢。很明显,我需要查字典,使这项工作做得更好。谢谢你们的代码示例谢谢你们。很明显,我需要查字典,使这项工作做得更好。谢谢你们两位的代码示例