python2134
python2134

Reputation: 21

Duplicate Values in For Loop

when I print "amount", I am getting duplicate values. Does this have to do with two for loops back to back on my first three lines?

missing_amount:

['102,131.49']

expected results:

102,131.49

actual results:

102,131.49
102,131.49

code: body_l = [] for link in url: body = browser.find_element_by_xpath("//[contains(text(), 'Total:')]").text body_l.append(body) icl_dollar_amount = re.findall('(?:[\£$\€]{1}[,\d]+.?\d)', body)[0].split('$', 1)[1] icl_dollar_amount_l.append(icl_dollar_amount) if not missing_amount: logging.info("List is empty") print("List is empty") count = 0 for amount in missing_amount: for count, item in enumerate(icl_dollar_amount_l): if amount == item: body = body_l[count] get_company_id = body.split("Customer Id:", 1)[1][4:10].strip() else: amount_ack_missing_l.append(amount) logging.info("Missing " + str(amount)) print("Missing " + str(amount))

With AgentJRock code:

when I print(icl_dollar_amount[i]) and missing_amount[i] my if statement never runs only the else runs. But both list have the same values, please see below.

for i in range(len(missing_amount)):
    print("Missing Amount : ", missing_amount[i])
    print("ICL DOLLAR", icl_dollar_amount_l[i])
    if missing_amount[i] == icl_dollar_amount_l[i]:
        body = body_l[i]
        get_company_id = body.split("Customer Id:", 1)[1][4:10].strip()
    else:
        amount_ack_missing_l.append(missing_amount[i])
        logging.info("Missing " + str(missing_amount[i]))
        print("Missing " + str(missing_amount[i]))

print(icl_dollar_amount[i]

ICL DOLLAR 2,760,127.58
ICL DOLLAR 325,845.84

print(missing_amount[i])

Missing Amount :  325,845.84
Missing Amount :  2,760,127.58

Upvotes: 0

Views: 1760

Answers (2)

AgentJRock
AgentJRock

Reputation: 43

Yes, you are correct, the inner loop is getting your missing_amount twice.

What type/values are in missing amount, icl_dollar_amount_l, and body_l?

Going off missing values is a list of ints and icl_dollar_amount_l is another list ints and bod_l is a dictionary

you might be best doing a single for loop to create an index from a range of the length of your lists. From this index in the for loop, you can compare indexes between two lists of shared indices. I think you're trying to share this index with your dict, which should be no problem with the same index we created

also, You have a count variable outside the for loop but have set another instance of count to the enumerate variable.

the outside count needs to be used with a (+=/-+) set to that variable which is equivalent to the enumerate and redundant.

for i in range(len(missing_amount)):
    if missing_amount[i] == icl_dollar_amount_l[i]:
        body = body_l[i]
        get_company_id = body.split("Customer Id:", 1)[1][4:10].strip()
    else:
        amount_ack_missing_l.append(missing_amount[i])
        logging.info("Missing " + str(missing_amount[i]))
        print("Missing " + str(missing_amount[i]))

Upvotes: 0

recursive_tree
recursive_tree

Reputation: 109

you do print("Missing " + str(amount)), but also logging.info("Missing " + str(amount)). I don't know what logging.info does, but i assume it prints to stdout. I'd recommend you to remove the print("Missing " + str(amount))

Upvotes: 1

Related Questions