[Tutor] Follow-up on my removing elements from lists question.
David L Neil
PyTutor at DancesWithMice.info
Sat Jun 15 17:03:00 EDT 2019
On 15/06/19 9:35 PM, mhysnm1964 at gmail.com wrote:
> 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.
(from a very weak understanding of your previous question and the total
code-base thus far)
Consideration nr1:
Write the code as comments first. Initially these will be at a fairly
'high level'. These comments can later be turned into function/method
names, and more comments added within those. Wash, rinse, and repeat.
The idea is to help re-state your thinking into Python code, and to
structure the code into functional units. Even skilled Python-coders
often find that this helps to keep the use-case foremost in-mind.
Consideration nr2:
(assuming that the total data-volume is easily RAM-resident)
Rather than (appearing to) taking-in a 'group' of transactions and then
select them according to ease/difficulty of 'translation', gradually
removing/whittling the numbers down - hopefully to zero; why not
consider adding another field to each record, which will note if it has
already been processed (by whichever method) or conversely, which
list-elements are yet to be completed? Thus each method of
interpretation will first check the 'completed' field, and if not
complete, apply the relevant analysis... Thus there is no concept of
'removal' and no danger of 'losing' anything!
--
Regards =dn
More information about the Tutor
mailing list