Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我在使用pop的范围内,但仍然得到一个超出范围的错误_Python - Fatal编程技术网

Python 我在使用pop的范围内,但仍然得到一个超出范围的错误

Python 我在使用pop的范围内,但仍然得到一个超出范围的错误,python,Python,当我使用re.findall时,它返回为[('0.76','22:43:11')]。当我尝试pop(1)或pop(0)时,我得到一个超出范围的错误。但我在射程之内 0.76表示一个电压值,另一个表示获取电压的时间 import re time = [('0.76', '22:43:11')] time1 = time.pop(1) print (time) print (time1) list.pop(1)将从列表中删除索引1处的项目,因为列表索引从0开始。列表中只有一项(

当我使用
re.findall
时,它返回为
[('0.76','22:43:11')]
。当我尝试
pop(1)
pop(0)
时,我得到一个超出范围的错误。但我在射程之内

0.76
表示一个电压值,另一个表示获取电压的时间

import re
time = [('0.76', '22:43:11')]
    
time1 = time.pop(1)
    
print (time)
print (time1)
list.pop(1)
将从列表中删除索引1处的项目,因为列表索引从0开始。列表中只有一项(元组)位于索引0处,因此
pop(1)
将失败

我认为你想深入研究元组,所以:

time = [('0.76', '22:43:11')]
time1 = time.pop(0)[1]    # take the first item from the list and get the second item in the returned tuple
print(time)
print(time1)
输出

[] 22:43:11 [] 0.76 22:43:11 输出

[] 22:43:11 [] 0.76 22:43:11 [] 0.76 22:43:11
time.pop(0)
工作正常,因为在
time
列表中,您有一个成员,索引为0,
('0.76','22:43:11')
是一个元组,它位于
time
列表的
索引0
中,而
索引1
中没有任何内容,因此您无法执行超出范围的
time.pop(1)

import re
time = [('0.76', '22:43:11')]

time1 = time.pop(0)

print (time)
print (time1)
上述代码的输出为:

[]
('0.76', '22:43:11')

pop(1)给出了超出范围的错误,因为只有1个元素len(time)等于1。pop(0)工作正常。

索引处的列表(元组)中只有一项
0
时间是一个成员列表。列表的长度为一。所以pop(1)尝试弹出无效的第二个元素。现在我看到了,谢谢,它在我的代码中工作得很好。我在列表中看到逗号,以为有两个元素,但实际上只有一个。