小白求助:关于pop弹出列表元素报错
代码如下:names = ['AAA','BBB','CCC']
names = 'DDD'
names.insert(0,'SSS')
names.insert(2,'WWW')
names.append('KKK')
names.pop(0)
name0 = names.pop(0)
names.pop(1)
name1 = names.pop(1)
names.pop(2)
name2 = names.pop(2)
问题1:在运行names.pop(0)后得到的结果为什么不是SSS而是DDD呢?对列表添加元素后这些元素难道不是重新排序吗?
问题2:运行到names.pop(2)报错:pop index out of range。然后我在执行pop操作前打印列表,列表显示['SSS', 'DDD', 'WWW', 'BBB', 'CCC', 'KKK'],说明前面的添加操作我应该是没出问题的呀,为什么会说我索引超出范围?
希望各位前辈不吝指教,鞠躬感谢! >>> names = ['AAA','BBB','CCC']
>>> names
['AAA', 'BBB', 'CCC']
>>> names = 'DDD'
>>> names
['DDD', 'BBB', 'CCC']
>>> names.insert(0,'SSS')
>>> names
['SSS', 'DDD', 'BBB', 'CCC']
>>> names.insert(2,'WWW')
>>> names
['SSS', 'DDD', 'WWW', 'BBB', 'CCC']
>>> names.append('KKK')
>>> names
['SSS', 'DDD', 'WWW', 'BBB', 'CCC', 'KKK']
>>> names.pop(0)
'SSS'
>>> names
['DDD', 'WWW', 'BBB', 'CCC', 'KKK']
>>> name0 = names.pop(0)
>>> names
['WWW', 'BBB', 'CCC', 'KKK']
>>> names.pop(1)
'BBB'
>>> names
['WWW', 'CCC', 'KKK']
>>> name1 = names.pop(1)
>>> names
['WWW', 'KKK']
>>> names.pop(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range names = ['AAA','BBB','CCC']
names = 'DDD'
print(f'1:{names}')
names.insert(0,'SSS')
print(f'2:{names}')
names.insert(2,'WWW')
print(f'3:{names}')
names.append('KKK')
print(f'4:{names}')
print()
names.pop(0)
print(f'5:{names}')
name0 = names.pop(0)
print(f'name0:{name0}')
print(f'6:{names}')
names.pop(1)
print(f'7:{names}')
name1 = names.pop(1)
print(f'name01:{name1}')
print(f'8:{names}')
names.pop(2)
print(f'9:{names}')
name2 = names.pop(2)
print(f'name2:{name2}')
print(f'10:{names}')
1:['DDD', 'BBB', 'CCC']
2:['SSS', 'DDD', 'BBB', 'CCC']
3:['SSS', 'DDD', 'WWW', 'BBB', 'CCC']
4:['SSS', 'DDD', 'WWW', 'BBB', 'CCC', 'KKK']
5:['DDD', 'WWW', 'BBB', 'CCC', 'KKK']
name0:DDD
6:['WWW', 'BBB', 'CCC', 'KKK']
7:['WWW', 'CCC', 'KKK']
name01:CCC
8:['WWW', 'KKK']
Traceback (most recent call last):
File "D:/python/test/test.py", line 21, in <module>
names.pop(2)
IndexError: pop index out of range
自己打印看一下names列表里还有什么元素不就好了么?
到了第8个names时,就剩下两个元素了,那当然就报错了,超出索引范围了 names = ['AAA','BBB','CCC']
names = 'DDD' #替换首项
print(names)
names.insert(0,'SSS') #插入作为首项其他后移
print(names)
names.insert(2,'WWW') #插入到第三项
print(names)
names.append('KKK') #插入到最后
print(names)
print("="*10) #分隔
print(names.pop(0))#这次弹出的是sss
print("剩余:",names)
name0 = names.pop(0) #再次弹出,是DDD
print("这次弹出的是:",name0)
print("剩余:",names)
print("弹出的第二项为:",names.pop(1)) #弹出第二项
print("剩余:",names)
name1 = names.pop(1)
print(name1)
本帖最后由 昨非 于 2020-10-12 16:17 编辑
弹出一项后,后面的项会自动补充上来,即下标是在变化的
所以上述代码的输出为:
['DDD', 'BBB', 'CCC']
['SSS', 'DDD', 'BBB', 'CCC']
['SSS', 'DDD', 'WWW', 'BBB', 'CCC']
['SSS', 'DDD', 'WWW', 'BBB', 'CCC', 'KKK']
==========
SSS
剩余: ['DDD', 'WWW', 'BBB', 'CCC', 'KKK']
这次弹出的是: DDD
剩余: ['WWW', 'BBB', 'CCC', 'KKK']
弹出的第二项为: BBB
剩余: ['WWW', 'CCC', 'KKK']
CCC 昨非 发表于 2020-10-12 16:14
感谢前辈,我明白错在哪里了,本来我是想把names.pop(0)直接赋值给name0,结果执行names.pop(0)后换行了,当再执行name0=names.pop(0)时就相当于又执行了一次pop操作。 兜里两颗糖 发表于 2020-10-12 16:25
感谢前辈,我明白错在哪里了,本来我是想把names.pop(0)直接赋值给name0,结果执行names.pop(0)后换 ...
对的,就是这个意思
页:
[1]