Python:一些列表访问问题
我添加了一个列表,如下所示:Python:一些列表访问问题,python,list,python-2.7,Python,List,Python 2.7,我添加了一个列表,如下所示: >>> l = [1,2,3,4] >>> len(l) 4 所以当我访问 l[3:1]python有返回空白列表,如[] l[3:1]=4返回如下错误 >>> l[3:1] = 4 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can only assign an
>>> l = [1,2,3,4]
>>> len(l)
4
所以当我访问
l[3:1]
python有返回空白列表,如[]
l[3:1]=4
返回如下错误
>>> l[3:1] = 4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only assign an iterable
现在我的长度是5。现在我想在第5个索引处添加新元素,所以我写
>>> l[5] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>>
>>l[5]=5
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
索引器:列表分配索引超出范围
>>>
我的问题如下:
l[3:1]=4时出现错误,但在使用l[3:1]=a时效果良好
由于
l[3:1]
是一个序列
,因此您只能为其分配一个序列
,
之所以
l[3:1]
起作用,是因为“a”本身是一个序列,而4
是一个正整数。因为l[3:1]
是一个序列
,因此您只能为其分配一个序列
,
之所以
l[3:1]
有效,是因为“a”本身是一个序列,而4
是一个普通整数。第一个问题-您试图从两种不同的方式分割列表
给定l=[1,2,3,4]
l[3::
从索引为3的元素开始切片列表,留下[4]
l[:1]
从索引为1的元素向后切片列表,留下[1]
现在,如果你从两个方向分割它,你将得到一个空列表,在这个范围内没有任何内容
第二个问题——这个问题有点奇怪。理论上,字符串的行为类似于一个列表,每个字符都是该列表中的一个项目,所以您只能输入“a”,而4是一个整数则会抛出一个错误。我不知道确切的原因,但是[3:1]
在第3个元素之后插入新的列表(字符串的字符)。如果您省略了[:1]
,它将替换第3个元素中的列表,留下[1,2,3,'a']
第三个问题-您只能访问已经存在的列表的索引。第五个索引还不存在。你必须创造它。这可以通过追加、插入等方式进行。然后您可以使用
l[5]
第一个问题对其进行修改-您正在尝试从两种不同的方式对列表进行切片
给定l=[1,2,3,4]
l[3::
从索引为3的元素开始切片列表,留下[4]
l[:1]
从索引为1的元素向后切片列表,留下[1]
现在,如果你从两个方向分割它,你将得到一个空列表,在这个范围内没有任何内容
第二个问题——这个问题有点奇怪。理论上,字符串的行为类似于一个列表,每个字符都是该列表中的一个项目,所以您只能输入“a”,而4是一个整数则会抛出一个错误。我不知道确切的原因,但是[3:1]
在第3个元素之后插入新的列表(字符串的字符)。如果您省略了[:1]
,它将替换第3个元素中的列表,留下[1,2,3,'a']
第三个问题-您只能访问已经存在的列表的索引。第五个索引还不存在。你必须创造它。这可以通过追加、插入等方式进行。然后您可以使用l[5]
list[i:j]表示您需要值从'i'索引到'j'索引的切片列表
(不包括第j个索引)。例如:
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list[2:3]
[3]
在您的情况下,i>j
,因此列表为空
l[3:1]
是切片列表。您只能使用列表指定它。因为,pythonstr
被认为是字符列表。这就是为什么您能够分配它
5
,但由于列表的索引以0开头,您只能访问列表的元素,直到len(list)-1
。如果要在列表末尾添加项,则需要在列表上执行.append()
。下面的示例说明了这一点:
>>> my_list = [1, 2, 7, 9, 8, 4, 5]
>>> len(my_list)
7 # <-- Length as 7
>>> my_list[6]
5 # <-- value of index at length(list) - 1, which is the last value of list
>>> my_list[7] # <-- index as length of list, resulted in 'IndexError'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> my_list.append(9) # <-- Adds an item at the of list
>>> len(my_list) # length incremented by 1
8
>>我的列表=[1,2,7,9,8,4,5]
>>>len(我的清单)
7#>>我的清单[6]
5#>>我的列表[7]#>>我的列表。追加(9)#>>len(我的列表)#长度增加1
8.
list[i:j]表示您需要值从'i'索引到'j'索引的切片列表
(不包括第j个索引)。例如:
>>> my_list = [1, 2, 3, 4, 5]
>>> my_list[2:3]
[3]
在您的情况下,i>j
,因此列表为空
l[3:1]
是切片列表。您只能使用列表指定它。因为,pythonstr
被认为是字符列表。这就是为什么您能够分配它
5
,但由于列表的索引以0开头,您只能访问列表的元素,直到len(list)-1
。如果要在列表末尾添加项,则需要在列表上执行.append()
。下面的示例说明了这一点:
>>> my_list = [1, 2, 7, 9, 8, 4, 5]
>>> len(my_list)
7 # <-- Length as 7
>>> my_list[6]
5 # <-- value of index at length(list) - 1, which is the last value of list
>>> my_list[7] # <-- index as length of list, resulted in 'IndexError'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> my_list.append(9) # <-- Adds an item at the of list
>>> len(my_list) # length incremented by 1
8
>>我的列表=[1,2,7,9,8,4,5]
>>>len(我的清单)
7#>>我的清单[6]
5#>>我的列表[7]#>>我的列表。追加(9)#>>len(我的列表)#长度增加1
8.
l[start:end] # start to end-1
l[start:] # start to rest of list
l[:end] # beginning of list to end-1
l[:] # copy complete list
当您通过l[3:1]
时,即从第三个索引开始,在第1-1个索引结束,即第0个索引,这意味着st