പൈത്തൺ നിഘണ്ടുക്കളുടെ രണ്ട് ലിസ്റ്റുകൾ താരതമ്യം ചെയ്യുക

ഈ പോസ്റ്റിൽ, പൈത്തണിലെ നിഘണ്ടുക്കളുടെ രണ്ട് ലിസ്റ്റുകൾ എങ്ങനെ താരതമ്യം ചെയ്യാമെന്നും രണ്ട് ലിസ്റ്റുകൾ തമ്മിലുള്ള വ്യത്യാസങ്ങൾ പ്രിന്റുചെയ്യാമെന്നും ഞങ്ങൾ നോക്കുന്നു.

താരതമ്യ രീതി കീകളെ താരതമ്യം ചെയ്യുന്നു ഒപ്പം നിഘണ്ടുവിലെ മൂല്യങ്ങൾ.

കൂടാതെ, പൈത്തണിലെ നിഘണ്ടുക്കളുടെ രണ്ട് ലിസ്റ്റുകൾ താരതമ്യപ്പെടുത്തുമ്പോൾ ഘടകങ്ങളുടെ ക്രമം പ്രശ്നമല്ല.




പൈത്തണിലെ നിഘണ്ടുക്കളുടെ പട്ടിക താരതമ്യം ചെയ്യുക

if __name__ == '__main__':
list_1 = [
{'id': '123-abc', 'name': 'Mike', 'age': 40},
{'name': 'John', 'age': 34, 'id': '123-efg'},
{'age': 32, 'id': '123-xyz', 'name': 'Aly'}
]
list_2 = [
{'name': 'Mike', 'id': '123-abc', 'age': 40},
{'id': '123-efg', 'age': 34, 'name': 'John'},
{'id': '123-xyz', 'name': 'Aly', 'age': 32}
]
assert [i for i in list_1 if i not in list_2] == []

മുകളിലുള്ള കോഡിൽ, list_1 ഒപ്പം list_2 തുല്യമാണ്. അതായത്, ഓരോ നിഘണ്ടുവിലും രണ്ട് ലിസ്റ്റുകളിലും ഒരേ ഇനങ്ങൾ (കീകളും മൂല്യങ്ങളും) അടങ്ങിയിരിക്കുന്നു. ഓരോ നിഘണ്ടുവിലെയും ഘടകങ്ങളുടെ ക്രമം അപ്രസക്തമാണ്.



നിഘണ്ടുക്കളുടെ പട്ടിക താരതമ്യം ചെയ്യുക - വ്യത്യാസങ്ങൾ അച്ചടിക്കുക

ലിസ്റ്റുകളിൽ ഏതൊക്കെ നിഘണ്ടു ഇനങ്ങൾ വ്യത്യസ്തമാണെന്ന് ഞങ്ങൾക്ക് അച്ചടിക്കാനും കഴിയും:


ഉദാഹരണത്തിന്:

if __name__ == '__main__':
list_1 = [
{'id': '123-abc', 'name': 'Mike', 'age': 40},
{'id': '123-efg', 'name': 'John', 'age': 24},
{'id': '123-xyz', 'name': 'Aly', 'age': 35}
]
list_2 = [
{'id': '123-abc', 'name': 'Mike', 'age': 40},
{'id': '123-efg', 'name': 'Jon', 'age': 24},
{'id': '123-xyz', 'name': 'Aly', 'age': 32}
]
for i in list_1:
if i not in list_2:

print(i)

Put ട്ട്‌പുട്ട്:

{'id': '123-efg', 'name': 'John', 'age': 24} {'id': '123-xyz', 'name': 'Aly', 'age': 35}

മുകളിലുള്ള രീതി എഴുതാനുള്ള മറ്റൊരു മാർഗ്ഗം:

def compare_two_lists(list1: list, list2: list) -> bool:
'''
Compare two lists and logs the difference.
:param list1: first list.
:param list2: second list.
:return:
if there is difference between both lists.
'''
diff = [i for i in list1 + list2 if i not in list1 or i not in list2]
result = len(diff) == 0
if not result:
print(f'There are {len(diff)} differences: {diff[:5]}')
return result


പാണ്ഡാസ് ഡാറ്റാ ഫ്രെയിം ഉപയോഗിച്ച് രണ്ട് ലിസ്റ്റുകൾ പരിവർത്തനം ചെയ്യുക

പാണ്ഡാസ് ഡാറ്റാഫ്രെയിം ഉപയോഗിച്ച് രണ്ട് ലിസ്റ്റുകൾ എങ്ങനെ താരതമ്യം ചെയ്യാമെന്ന് ചുവടെയുള്ള ഉദാഹരണ കോഡ് കാണിക്കുന്നു


from pandas import DataFrame import pandas as pd def compare_two_lists(list1: list, list2: list) -> bool:
'''
Compare two lists and logs the difference.
:param list1: first list.
:param list2: second list.
:return:
if there is difference between both lists.
'''
df1 = pd.DataFrame(list1)
df2 = pd.DataFrame(list2)
diff = dataframe_difference(df1, df2)
result = len(diff) == 0
if not result:
print(f'There are {len(diff)} differences: {diff.head()}')
return result def dataframe_difference(df1: DataFrame, df2: DataFrame) -> DataFrame:
'''
Find rows which are different between two DataFrames.
:param df1: first dataframe.
:param df2: second dataframe.
:return: if there is different between both dataframes.
'''
comparison_df = df1.merge(df2, indicator=True, how='outer')
diff_df = comparison_df[comparison_df['_merge'] != 'both']
return diff_df

രസകരമായ ലേഖനങ്ങൾ