list = [0, 1, 2, 8, 2, 9, 2]
有没有办法删除元素2
,只有一次?
所以你会得到:
list = [0, 1, 2, 8, 9, 2]
我尝试使用index()
,但我没有找到它。
它可以是随机2
。
所以我无法使用remove()
或pop()
,因为它不会删除随机位置上的数字2
。
答案 0 :(得分:6)
这有效
list.remove(2)
L.remove(value) - 删除第一次出现的值。
如果值不存在,则引发ValueError。
答案 1 :(得分:1)
使用del
或pop
例如,
del list[2]
或
list.pop(2)
del和pop之间的区别在于
del
已超载。
例如,del a [1:3]表示删除元素1和3
答案 2 :(得分:1)
随机删除<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="841.89px" height="595.28px" viewBox="0 0 841.89 595.28" enable-background="new 0 0 841.89 595.28" xml:space="preserve">
<g>
<path class="firstpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="
M253.441,284.167c0,0-0.883-14.535,15.429-18.551c5.364-1.32,15.943-0.665,21.667,6.79c13.859,18.051-3.235,32.286-8.087,37.262
c-4.853,4.979-7.713,21.027-17.543,20.528c-9.829-0.497-12.69-6.717-12.317-11.818" />
<path class="secondpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="
M285.249,285.252c0,0,0.654-8.212-8.864-10.544c-9.519-2.334-16.796,8.211-10.825,17.449c2.613-1.12,5.226,0.652,5.599,2.52
c0.373,1.866-1.213,4.199-3.826,4.199" />
<path class="thirdpath" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" d="
M257.815,307.553c0,0-3.732,5.879,2.333,8.118c6.065,2.241,5.318-8.118,8.865-9.237c3.545-1.119,11.943-2.519,11.943-10.265
c0-7.744-6.398-16.451-17.522-11.958" />
</g>
<line class="row1col1" fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="287.987" x2="324.156" y2="286.627" />
<line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="285.335" x2="332.744" y2="284.167" />
<line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="338.25" y1="301.171" x2="343.41" y2="301.171" />
<line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="328.667" y1="301.171" x2="333.75" y2="301.171" />
<line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="319.41" y1="301.171" x2="324.156" y2="301.171" />
<line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="313.741" y1="314.625" x2="319.41" y2="315.987" />
<line fill="none" stroke="#000000" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="10" x1="323.741" y1="316.746" x2="329.667" y2="318.142" />
</svg>
备注:强>
2
的索引列表,即2
<强>代码:强>
[i for i, j in enumerate(lst) if j == 2]
<强>输出:强>
import random
lst = [0, 1, 2, 8, 2, 9, 2]
lst.pop(random.choice([i for i, j in enumerate(lst) if j == 2]))
print lst
答案 3 :(得分:0)
请注意,您正在隐藏内置list
。除此之外index
工作正常:
>>> li = [0, 1, 2, 8, 2, 9, 2]
>>> li.pop(li.index(2))
2
>>> li
[0, 1, 8, 2, 9, 2]