Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编辑特定列表元素_Python_List - Fatal编程技术网

Python 编辑特定列表元素

Python 编辑特定列表元素,python,list,Python,List,我正在尝试编辑列表中的特定元素。但是,在正确修改列表元素后,例如将\n6.5%\n转换为6.5%,我无法用新元素替换旧元素。因此,\n6.5%\n和所有其他元素在脚本结束时保持不变 下面是一个可复制的示例 #Define List example example_list = ['<tr>\n<th><a href="/7.62x25mm_TT_AKBS" title="7.62x25mm TT AKBS"><im

我正在尝试编辑列表中的特定元素。但是,在正确修改列表元素后,例如将
\n6.5%\n
转换为
6.5%
,我无法用新元素替换旧元素。因此,
\n6.5%\n
和所有其他元素在脚本结束时保持不变

下面是一个可复制的示例

#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

这似乎是在处理一个简单列表之外的其他内容?。老实说,我不能真正理解开头的问题或目标是什么


不再相关,但在另一个答案中解决。

你也会注意到我使用

        if (character.isnumeric()) or (character == '+') or (character == '-') or (character == '%') or (character == '.'):
我相信这会让你们中的一些人退缩。我确实尝试了一些事情

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

但这并不奏效。如果有一个简单明了的方法来写这篇文章,我希望你能给我指出正确的方向。

请尝试使用
中的
操作符:

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

你的问题是什么?每个问题一个问题。是否有可能将代码缩减到最低程度?如果您现在正在使用IDE,那么现在正是学习其调试功能的好时机,如单步执行、设置断点和检查值。或者你可以花一点时间熟悉内置的。此外,在程序的关键点打印内容可以帮助您跟踪正在发生或未发生的事情。您是否正在询问如何修改for循环中的列表项?如果是这样的话,有很多可能的重复。(i)是的,我熟悉MRE,但我会因为我糟糕的If声明而受到指责。我承认,我本可以缩减10项清单,但老实说,我并不认为它的长度太长(ii)我包含了品脱语句,因此我知道我编辑正确,这是我无法做到的“覆盖”。我可以简单地选择一个变量并检查其中的内容。但我无法确定为什么列表元素没有改变(iii)特定项目,是的。我在C语言中发现了一些重复的语言,比如lang和java,但不是Python?
if (character.isnumeric()) or (character in ['+','-','.','%']):