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)
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.