如何让乘法键都存在于字典中? (python)
How to let the multiply keys all exist in dictionary? (python)
我有问题。
我想添加相同的记录,但旧记录将被新记录取代!我希望它们都存在,这样我就可以看到我做了什么。
我该如何解决这个问题?请帮助我!
谢谢!
我的代码:
initial_money = int(input('How much money do you have? '))
mr={}
add_mr={}
def records():
return
def add(records):
records = input('Add an expense or income record with description and amount:\n').split()
global mr
global rec
rec = records[0]
global amt
amt = records[1]
for r, a in mr.items():
i = 0
while (r, i) in add_mr:
i += 1
add_mr[(r, i)] = a
global initial_money
mr[rec] = int(amt)
initial_money += mr[rec]
def view(initial_money, records):
print("Here's your expense and income records:")
print("Description Amount")
print("------------------- ------")
for r,a in mr.items():
print('{name:<20s} {number:<6s}'.format(name = r,number = str(a)))
print('Now you have {} dollars.'.format(initial_money))
while True:
command = input('\nWhat do you want to do (add / view / delete / exit)? ')
if command == 'add':
records = add(records)
elif command == 'view':
view(initial_money, records)
输出:
How much money do you have? 1000
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
ewq 87
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
ewq 87
What do you want to do (add / view / delete / exit)? view
Here's your expense and income records:
Description Amount
------------------- ------
tree 87
Now you have 1174 dollars.
我想要的输出:
------------------- ------
tree 87
tree 87
与其使用 Mr = {"ewq": 87}
这样的字典,不如使用包含所有这些的列表 Mr = [("ewq", 87), ("ewq", 87)]
这样一来,即使您有副本,也不会被覆盖。因为字典每个键只能有一个
编辑:您可以通过将 mr = {}
的第一部分替换为 mr = []
来做到这一点,然后每当您调用 addmr
时,您可以只使用 mr.append(value)
将值添加到列表
你实际上可以用字典来做到这一点。每个键使用一个列表,而不是每个键一个项目。
if rec not in mr:
mr[rec] = []
mr[rec].append(amt)
这样 mr = {"ewq": [87, 87]}
我有问题。
我想添加相同的记录,但旧记录将被新记录取代!我希望它们都存在,这样我就可以看到我做了什么。
我该如何解决这个问题?请帮助我!
谢谢!
我的代码:
initial_money = int(input('How much money do you have? '))
mr={}
add_mr={}
def records():
return
def add(records):
records = input('Add an expense or income record with description and amount:\n').split()
global mr
global rec
rec = records[0]
global amt
amt = records[1]
for r, a in mr.items():
i = 0
while (r, i) in add_mr:
i += 1
add_mr[(r, i)] = a
global initial_money
mr[rec] = int(amt)
initial_money += mr[rec]
def view(initial_money, records):
print("Here's your expense and income records:")
print("Description Amount")
print("------------------- ------")
for r,a in mr.items():
print('{name:<20s} {number:<6s}'.format(name = r,number = str(a)))
print('Now you have {} dollars.'.format(initial_money))
while True:
command = input('\nWhat do you want to do (add / view / delete / exit)? ')
if command == 'add':
records = add(records)
elif command == 'view':
view(initial_money, records)
输出:
How much money do you have? 1000
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
ewq 87
What do you want to do (add / view / delete / exit)? add
Add an expense or income record with description and amount:
ewq 87
What do you want to do (add / view / delete / exit)? view
Here's your expense and income records:
Description Amount
------------------- ------
tree 87
Now you have 1174 dollars.
我想要的输出:
------------------- ------
tree 87
tree 87
与其使用 Mr = {"ewq": 87}
这样的字典,不如使用包含所有这些的列表 Mr = [("ewq", 87), ("ewq", 87)]
这样一来,即使您有副本,也不会被覆盖。因为字典每个键只能有一个
编辑:您可以通过将 mr = {}
的第一部分替换为 mr = []
来做到这一点,然后每当您调用 addmr
时,您可以只使用 mr.append(value)
将值添加到列表
你实际上可以用字典来做到这一点。每个键使用一个列表,而不是每个键一个项目。
if rec not in mr:
mr[rec] = []
mr[rec].append(amt)
这样 mr = {"ewq": [87, 87]}