[Tutor] Follow-up on my removing elements from lists question.

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Sat Jun 15 05:35:57 EDT 2019


This is a follow-up on my previous question for removing elements. Below is
the code I am currently using. I am removing the elements at the end of the
outer loop. The data structure goes along this:

 

[ 

  ['123123',[2019-2-18', 'transaction text', 'amount'],

v  ['123123',[2019-2-18', 'transaction text', 'amount'],

  ['123123',[2019-2-18', 'transaction text', 'amount']

]

 

The 2nd column where the transaction text I am modifying the content and
using the end result of the built-up words as the string match as you will
see in the code. This is all working fine. The last loop in the code I am
trying to delete the elements in reverse order. This doesn't work. The
length of the list reduces by 1. When it should have reduced by 42. Is the
logic wrong? This is in Python 3.6 under windows 10.

 

unknown_transactions.sort(key=lambda x: x[2])

while True:

# re-initialise each time the current transaction text has been processed.

    for row in unknown_transactions:

        # remove common words from transactions which are not required. Such
as 'WITHDRAWAL' and 'DEPOSIT'.

        line = regex_transaction(row[2])

        # If the common words are not found, return a null and do not modify
the transaction description.

        if line != '': # not a null string

            # now find unique string and add it to the compare_transactions
list object.

            words = line.split()

            print ('List length:', len(unknown_transactions))

            word = ''

            for i, v in enumerate(words, start=1):

                word = ' '.join(words[:i])

                print (word)

                answer = input('Use  word y, otherwise any other key
continues...')

                if answer != 'y':

                    continue

                # end if 

            # end for 

            # now loop through the unknown transactions and copy to
transaction dictionary

            delete_transactions = []

            for e, v in enumerate (unknown_transactions):

                if word in unknown_transactions[e][2]:

                    if not word  in transaction:

                        transaction[word] = unknown_transactions

                    else:

                        transaction[word].append(unknown_transactions)

                    # end if  

                    delete_transactions.append (e)

                # end if 

            # end for 

            print ('number of elements to remove:',
len(delete_transactions))

            for del_element in reversed(delete_transactions):

                unknown_transactions.pop(del_element)

        # end if 

    # end for 

    if len(unknown_transactions) == 0:

        break

    # end if 

# end while



More information about the Tutor mailing list