Home > Software design >  How can I compare two lists and m python. - Capture the different value
How can I compare two lists and m python. - Capture the different value

Time:11-03

I am trying to compare two lists in python, one of them is a response from a rest request that I stored in a list and the other is obtained through a csv file. I need to compare them and capture the values that do not exist in the first list that is obtained from the csv that is smaller than the second list that is the response from my database.

Just to summarize, the values that are in the csv are checked to ensure that everything has been stored correctly and exists in the database.

So far I have built this to do the comparisons, however I am getting a lot of repeated values in my console, it is printing many times the same value.

import json, requests, urllib.parse, re
from jsonpath_ng import jsonpath, parse
from pandas.io.parsers import read_csv
import pandas as pd
from termcolor import colored
import numpy as np
from glob import glob

# Set Up
dateinplay = "2021-09-27"
cdwenv1 = "cdwu"  # Note that it only works with the http version right now
cdwenv2 = "cdwp"  # Control Env, usually cdwp
CoreMurexFilesLoc = r"J:\Gerard\Release197\UAT\day1\Files\Core"

# Dev Static
cdwenv = ""  # leave empty
files = glob(CoreMurexFilesLoc   "\\*")

# index 1: MHEU_TradeCash_ | 2: TradeCash_
tradeCashfiles = [i for i in files if "TradeCash" in i]

# index 1: MHEU_Trade_ | 2: Trade_
tradeFiles = [i for i in files if "Trade_" in i]

mheu_tradeCash = tradeCashfiles[1]
# tradeCash = tradeCashfiles[2]
# mheu_trade = tradeFiles[1]
# trade = tradeFiles[2]

# filesList = [mheu_trade, trade, mheu_tradeCash, tradeCash]


def read_csv(file):
    df_trade = pd.read_csv(
        file, delimiter="|", index_col=False, low_memory=False, dtype="unicode"
    )

    # Drop any blank fields
    df_trade.dropna(subset=["MurexCounterpartyRef"], inplace=True)
    tradeList = df_trade["MurexCounterpartyRef"]

    # remove elemets duplicates
    l = []
    for i in tradeList:
        if i not in l:
            l.append(i)
    l.sort()
    return l


mheu_tradeCash = read_csv(mheu_tradeCash)
# tradeCash = read_csv(tradeCash)
# mheu_trade = read_csv(mheu_trade)
# trade = read_csv(trade)

# Request to get the accountID related to the date
listAccountId = []
cdwCounterparties = f"http://cdwu/cdw/counterparties/{dateinplay}?limit=999999"
r = requests.get(cdwCounterparties).json()

jsonpath_expression = parse("$..accounts.account[*].identifiers.identifier[*]")

for match in jsonpath_expression.find(r):
    # print(f'match id: {match.value}')
    thisdict = match.value
    if thisdict["accountIdType"] == "ACCOUNTID":
        #   print(thisdict["accountId"])
        listAccountId.append(thisdict["accountId"])

# print the quantity of accountID found in the CDW
# print(len(listAccountId))


# Compare the two lists: If the values found in Murex -> csv exist in list of accountId -> cdw
def comparator(murex, cdw):
    for i in murex:
        for j in cdw:
            if i != j:
                print(f"The counterparty {i} does not exist in CDW")


# mheu_tradeCash | listAccountId
comparator(mheu_tradeCash, listAccountId)

Can anyone help me with this?

CodePudding user response:

I think that this is answered here, How to compare a list of lists/sets in python?

For this specific case you can do something like this,

difference = list(set(mheu_tradeCash) - set(listAccountId))

[print(f"The counterparty {i} does not exist in CDW") for i in difference]
  • Related