可用删除(a)不工作

2024-04-19 06:42:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从Python的列表中删除元素。你知道吗

AvailableLetters = ['a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z']
AvailableLetters.remove('a')

我得到以下错误

AvailableLetters.remove('a')
ValueError: list.remove(x): x not in list

Tags: in元素列表错误notremovelistvalueerror
3条回答

Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.

>>> 'Py' 'thon'
'Python'

您的问题是python将'a''b'视为'ab'(可能是C的回溯)。你想要的是:

AvailableLetters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
AvailableLetters.remove('a')
>>> import string
>>> letters = list(string.ascii_lowercase)
>>> letters.remove('a')
>>> print(letters)
['b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

相关问题 更多 >