下面的代码将两个字典转换成一个列表:
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)
当我运行它时,我得到以下输出:
['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}].................................]
但是我需要以下输出:
[{'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},
............................................]
就像@anishtain说的,我希望你能提供更多关于你的情况的代码/信息。不管怎样,我还是尽力帮助你。
基于最终结果和你的代码,我想出了这个样本数据。这无论如何都不准确。
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}
}
解决方案# 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])
添加视觉效果后的结果会是什么样子
在没有颜色的终端中结果应该是什么样子。这仍然有点混乱,但没有冗余的数据。
解决方案#2 -使用字典列表
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)
在终端中应该是什么样子
这可能不是最好的解决方案,但我希望它能有所帮助。