简体   繁体   中英

how to print a list in below format

The following code is converting two dictionaries into a list:

payment_no=[]
for i in df2:
    payment_list=[]
    for k in df3:   
        if df2[i]['Payment_Number'] == df3[k]['Payment_Number']:
            payment_list.append(df3[k])
    payment_no.append('Payment_No:'+df2[i]['Payment_Number'])
    payment_no.append(payment_list)
print(payment_no)

When I run it, I get the below output:

['Payment_Number:197330238',
[{'Payment_Number': '197330238', 'Invoice Number': '80600013156', 'Invoice Date': '5/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments',}, {'Payment_Number': '197330238', 'Invoice Number': '80600013140', 'Invoice Date': '4/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments',}, {'Payment_Number': '197330238', 'Invoice Number': '80600013182', 'Invoice Date': '6/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments', }],
'Payment_Number:197330238',
[{'Payment_Number': '197330295', 'Invoice Number': '61700018202-TDS-CM-7855', 'Invoice Date': '4/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan}, {'Payment_Number': '197330295', 'Invoice Number': '61700018247-TDS-CM-7302', 'Invoice Date': '5/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan, }, {'Payment_Number': '197330295', 'Invoice Number': '64100010621-TDS-CM-2516', 'Invoice Date': '6/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan}\].................................]

but i need the following output:

[{'Payment_Number': '197330238', Line:{'Invoice Number': '80600013156', 'Invoice Date': '5/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments'}, { 'Invoice Number': '80600013140', 'Invoice Date': '4/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments'}, { 'Invoice Number': '80600013182', 'Invoice Date': '6/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments'},
{'Payment Number':'197330295' ,Line:[{'Invoice Number': '61700018202-TDS-CM-7855', 'Invoice Date': '4/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan}, { 'Invoice Number': '61700018247-TDS-CM-7302', 'Invoice Date': '5/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan}, { 'Invoice Number': '64100010621-TDS-CM-2516', 'Invoice Date': '6/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan},
............................................]

Like @anishtain said, I wish you would have provided more code/information about your situation. Regardless, I still tried to help you.

Based on the final result and your code, I came up with this for the sample data. Its not accurate by any means.

df2 = {
    0 : {'Payment_Number': '197330238'} ,
    1 : {'Payment_Number': '197330238'} ,
    2 : {'Payment_Number': '197330238'} ,
    3: {'Payment_Number': '197330238'},
    4: {'Payment_Number': '197330295'},
    5: {'Payment_Number': '197330295'},
    6: {'Payment_Number': '197330295'}
}

nan = 0 # temp value;  may not be accurate

df3 = {
    0: {'Payment_Number': '197330238', 'Invoice Number': '80600013182', 'Invoice Date': '6/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments'},
    1: {'Payment_Number': '197330238', 'Invoice Number': '80600013156', 'Invoice Date': '5/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments'},
    2: {'Payment_Number': '197330238', 'Invoice Number': '80600013140', 'Invoice Date': '4/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments'},
    3: {'Payment_Number': '197330238', 'Invoice Number': '80600013182', 'Invoice Date': '6/2/2022', 'Transaction type': 'Invoice', 'Transaction Description': 'Payments'},
    4: {'Payment_Number': '197330295', 'Invoice Number': '61700018202-TDS-CM-7855', 'Invoice Date': '4/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan},
    5: {'Payment_Number': '197330295', 'Invoice Number': '61700018247-TDS-CM-7302', 'Invoice Date': '5/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan},
    6: {'Payment_Number': '197330295', 'Invoice Number': '64100010621-TDS-CM-2516', 'Invoice Date': '6/2/2022', 'Transaction type': 'Others', 'Transaction Description': nan}
}

Solution #1:

payment_no = []
unique_no = set(df2[i]['Payment_Number'] for i in df2)  # takes all unique numbers from dicts in list
for num in unique_no:
   payment_list = []
   for k in df3:   
       if num == df3[k]['Payment_Number']:
           # prevents the "Payment_Number" key from being removed from df3 permanently
           payment_list.append({key:value for key,value in df3[k].items() if key != 'Payment_Number'}) 
   payment_no.append('Payment_No: '+ num)
   payment_no.append(payment_list)

# skips every two indexes because those are pairs
for i in range(0, len(payment_no), 2):
   print(payment_no[i], 'Lines:',payment_no[i+1])

What the result would look like with added visuals

What the result should look like in terminal without the color. This is still somewhat messy but does not have redundant data.

Solution #2 - use a list of dictionaries

payment_no = []
unique_no = set(df2[i]['Payment_Number'] for i in df2)  # takes all unique numbers from dicts in list
for num in unique_no:
    payment_list = []
    for k in df3:   
        if num == df3[k]['Payment_Number']:
            # prevents the "Payment_Number" key from being removed from df3 permanently
            payment_list.append({key:value for key,value in df3[k].items() if key != 'Payment_Number'}) 
    
    temp = {'Payment_No': num, 'Lines': payment_list}
    payment_no.append(temp)

for d in payment_no:
    print(d)

What the result should look like in terminal.

This is probably not the best solution, but I hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM