Faʻatusatusa Python Lisi Lua o Lomifefiloi

I lenei pou, matou te tilotilo ai pe faʻafefea ona faʻatusatusa ni lisi se lua o lomifefiloi i le Python ma lolomiina foʻi le eseʻesega i le va o lisi e lua.

O le faʻatusatusaina metotia faʻatusatusa ki ma tulaga faatauaina i lomifefiloi.

E le gata i lea, o le okaina o elemeni e le afaina pe a faʻatusatusa lua lisi o lomifefiloi i le Python.

Faʻatusatusa lisi o lomifefiloi i le Python

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] == []

I le numera i luga atu, list_1 ma list_2 e tutusa. O lona uiga, o lolomifefiloi uma e aofia ai aitema tutusa (ki ma tulaga taua) i lisi uma e lua. O le faʻasologa o elemeni i tusi taʻitasi taʻitasi e le talafeagai.

Faʻatusatusa lisi o lomifefiloi - lolomitusi eseesega

E mafai foi ona matou lolomia poʻo a mea lolomifefiloi e eseese i lisi:

Faataitaiga:



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)

Galuega Faatino:

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

O le isi auala e tusi ai le auala i luga o le:

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

Lisi Lisi Lua Faʻaaogaina Pandas DataFrame

O le faʻataʻitaʻiga code i lalo o loʻo faʻaalia ai le faʻatusatusaina o lisi e lua e faʻaaoga ai Pandas DataFrame

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