파이썬에서 목록에서 숫자를 제거하는 방법

코드 예제

88
0

파이썬 목록에서 요소 제거

myList.remove(item) # Removes first instance of "item" from myList
myList.pop(i) # Removes and returns item at myList[i]
13
0

목록 파이썬에서 값 삭제

list.remove(element)
11
0

파이썬 목록에서 항목을 제거하는 방법

list.remove(item)
2
0

파이썬 목록에서 값 제거

# Below are examples of 'remove', 'del', and 'pop' 
#   methods of removing from a python list
""" 'remove' removes the first matching value, not a specific index: """
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]

""" 'del' removes the item at a specific index: """
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]

""" 'pop' removes the item at a specific index and returns it. """
>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]

""" Their error modes are different too: """
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: pop index out of range
1
0

파이썬 목록에서 항목 삭제

l = list(range(10))
print(l)
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

del l[0]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

del l[-1]
print(l)
# [1, 2, 3, 4, 5, 6, 7, 8]

del l[6]
print(l)
# [1, 2, 3, 4, 5, 6, 8]

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................