0

I'm trying to edit specific elements in a list. However after correctly modifying the list element, for example converting \n<td>6.5%\n to 6.5%, I am failing to replace the old element with the new one. As such, \n<td>6.5%\n and all the other elements remain unchanged by the end of my script.

Below is a replicable example.

#Define List example
example_list = ['<tr>\n<th><a href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS"><img alt="TTAKBS.png" decoding="async" height="64" src="https://static.wikia.nocookie.net/escapefromtarkov_gamepedia/images/6/61/TTAKBS.png/revision/latest/scale-to-width-down/64?cb=20190519001904" width="64"/></a>\n</th>\n<th><a href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS">7.62x25mm TT AKBS</a>\n</th>\n<td>58\n',
         'h>\n<td>58\n',
         '\n<td>12\n',
         '\n<td>32\n',
         '\n<td><font color="green">+15</font>\n',
         '\n<td><font color="green">-15</font>\n',
         '\n<td>25%\n',
         '\n<td>6.5%\n',
         '\n<td>425\n',
         '\n<td>\n',
         '\n<td data-sort-value="1"><a href="/Prapor" title="Prapor">Prapor</a> LL1\n',
         '</tr>']

#`for loop` that only looks as the elements I want to clean
for cluttered_digits in example_list[1:9]:

    #`for loop` and `if statment` that only retains a character if it is a digit,
    #or relevant mathematical symbol (`.`, `+`, `-`, `%`)
    cleaned_digits = ''
    for character in cluttered_digits:
                if (character.isnumeric()) or (character in ['+','','.','%']):
    print('cleaned digits: ' + cleaned_digits) # reveals partial success

    # (i)
    #Here I attempt to replace the old list element with the new, cleaned one.
    cluttered_digits = cleaned_digits

    # (ii)
    #The above failed so I also tried this to see what would happen.
    for field in example_list:
        if field == cluttered_digits:
            field = str(cleaned_digits)


print(example_list) # reveals failure

Attempted solutions:

python modify item in list, save back in list

#This approach removes the first list element, and also duplicates another?

for index, cluttered_digits in enumerate(example_list[1:9]):

cleaned_digits = ''
for character in cluttered_digits:
    if (character.isnumeric()) or (character in ['+','','.','%']):
        cleaned_digits += character
        print('cleaned digits: ' + cleaned_digits)

example_list[index] = cleaned_digits

print(example_list)

Modify a list while iterating

This appears to be dealing with something other than a simple list?. Honestly, I can't really follow the opening question or what the goal is.


No Longer Relevant, but addressed in an alternate answer by Wasif Hasan.

You will also note that I use...

        if (character.isnumeric()) or (character == '+') or (character == '-') or (character == '%') or (character == '.'):

Which I'm sure is making some of you wince. I did try a few things...

        if (character.isnumeric()) or (character == ['+','-','.','%']):

But this did not work. If there is an obvious and easy way of writing this I would appreciate being pointed in the right direction.

Solebay Sharp
  • 519
  • 7
  • 24
  • Which is your question? One question per question. Any chance of reducing your code to a minimal [mre]? – wwii Nov 15 '20 at 15:46
  • If you are using an IDE **now** is a good time to learn its debugging features - like stepping through execution, setting breakpoints, and examining values. Or you could spend a little time and get familiar with the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Also, printing *stuff* at strategic points in your program can help you trace what is or isn't happening. – wwii Nov 15 '20 at 15:47
  • Are you asking how to modify list items in a for loop? If so, there are quite a few possible duplicates. – wwii Nov 15 '20 at 15:58
  • (i) Yes I am familiar with the MRE but I was going to catch flak for my terrible if statement. I could have curtailed the 10 item list I admit, but honestly I didn't think its lenght was overly grievous(ii) I have included pint statements, hence I know I am editing correctly, it is the 'overwriting' I am not able to do. I can simply select a variable and check what is in it as well. But I could not ascertain why the list element was unchanged (iii) Specific items, yes. I found a few duplicates in C-like lang and java but not Python. – Solebay Sharp Nov 15 '20 at 16:08
  • Do any of these answer your question? [Modify a list while iterating](https://stackoverflow.com/questions/44864393/modify-a-list-while-iterating), [How to modify list entries during for loop?](https://stackoverflow.com/questions/4081217/how-to-modify-list-entries-during-for-loop), [python modify item in list, save back in list](https://stackoverflow.com/questions/6130211/python-modify-item-in-list-save-back-in-list) ? – wwii Nov 15 '20 at 16:10
  • I will remove some print statments to reduce the length and clutter. – Solebay Sharp Nov 15 '20 at 16:10
  • Is it safe to say that `example_list[1:9]` should be `['58', '12', '32', '+15', '-15', '25%', '6.5%', '425']` after the process and all other items remain the same? – wwii Nov 15 '20 at 16:17
  • Yes. Your first link confuses me I am unsure of what is being done. The second link (I am about to append my results) seems to cut off the first list element for some reason which I still need, I just don't want to submit it to processing editing. Obviously I must still explore your subsequent links. – Solebay Sharp Nov 15 '20 at 16:22
  • Using one method shown in the *duplicates*: `for i, cluttered_digits in enumerate(example_list[1:9],start=1): ... for character in cluttered_digits: ... ;example_list[i] = cleaned_digits` where `example_list[i] = cleaned_digits` is *after* the `for character in cluttered_digits:` loop stops - same indentation as the `for character in cluttered_digits:` statement. – wwii Nov 15 '20 at 16:22
  • Yes, this does not seem to get me my desired results as it destroys list elements and duplicates others. – Solebay Sharp Nov 15 '20 at 16:29
  • @wii I retract that, `start = 1` means that it now works. – Solebay Sharp Nov 15 '20 at 16:39

1 Answers1

1

Try with in operator:

if (character.isnumeric()) or (character in ['+','-','.','%']):
Wasif
  • 14,755
  • 3
  • 14
  • 34