Untitled

mail@pastecode.io avatar
unknown
python
7 months ago
1.4 kB
1
Indexable
Never
# Alternativ för att slå ihop två tabeller / dicts, med key-value till key-value-value

debitItem1 = {'6230': 79.2, '2640': 19.8}
creditItem1 = {'1930': 98, '6230': 1}

debitItem2 = {'1510': 22183}
creditItem2 = {'3010': 17746.4, '2610':4436.6}

print("Alternativ 1")
accounts_item1 = {}

# Alternativ 1: 
# Skapa en dict = {account, {credit_amount, debit_amount}}

for item in debitItem1.items():
    accounts_item1[item[0]] = [item[1],'']


for item in creditItem1.items():
    if item[0] in accounts_item1:
        accounts_item1[item[0]] = [accounts_item1[item[0]][0], item[1]]
    else:
        accounts_item1[item[0]] = ['',item[1]]

print(accounts_item1)
'''
{'6230': [79.2, 1], '2640': [19.8, ''], '1930': ['', 98]}
'''

print("Alternativ 2")
# Alternativ 2: 
# Skapa en list av list = 
# [
#  [account1, debit_amount, credit_amount],
#  [account2, debit_amount, credit_amount],
#  [account3, debit_amount, credit_amount]
# ]

accounts_item1 = []

for item in debitItem1.items():
    accounts_item1.append([item[0], item[1],''])

for item in creditItem1.items():
    inRow = False
    for row in accounts_item1:
        if row[0] == item[0]:
            row[2] = item[1]
            inRow = True
            break
    if not inRow:
        accounts_item1.append([item[0],'',item[1]])

print(accounts_item1)

'''
[['6230', 79.2, 1], ['2640', 19.8, ''], ['1930', '', 98]]
'''