如何让乘法键都存在于字典中?(python)

如何让乘法键都存在于字典中?(python),python,dictionary,key,Python,Dictionary,Key,我有个问题 我想添加相同的记录,但旧的将被一个新的取代!我希望他们都存在,这样我就可以看到我做了什么 我如何解决这个问题?请帮帮我 谢谢大家! 我的代码: 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

我有个问题

我想添加相同的记录,但旧的将被一个新的取代!我希望他们都存在,这样我就可以看到我做了什么

我如何解决这个问题?请帮帮我

谢谢大家!

我的代码:

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)
我想要的输出:

-------------------  ------
tree                  87
tree                  87    

您可以使用一个包含所有这些内容的列表,而不是使用一个口述词,例如
Mr={“ewq”:87}
,以便
Mr=[(“ewq”,87),(“ewq”,87)]

这将使它,如果你有一个重复,它不会覆盖。因为字典每个键只能有一个


编辑:你可以用
mr=[]
替换第一个写着
mr={}
的部分,然后每当你调用
addmr
时,你可以使用
mr.append(value)
将值添加到列表中,实际上你可以用dict来做这件事,而不是每个键使用一个列表

if rec not in mr:
    mr[rec] = []
mr[rec].append(amt)

这样的话,
mr={“ewq”:[87,87]}

你不能用dict做那件事
if rec not in mr:
    mr[rec] = []
mr[rec].append(amt)