Python - 如何从列表中完全清除整数?

时间:2011-02-08 02:10:35

标签: python python-3.x

# Assign list "time" with the following time values.    
time = [15, 27, 32, 36.5, 38.5, 40.5, 41.5, 42, 43.5, 45.5, 47.5, 52.5]
# Remove 1st value(0) from the list
time[0] = []
# Show time
time
[[], 27, 32, 36.5, 38.5, 40.5, 41.5, 42, 43.5, 45.5, 47.5, 52.5]
# Print time 
print(time)
[[], 27, 32, 36.5, 38.5, 40.5, 41.5, 42, 43.5, 45.5, 47.5, 52.5]

到目前为止,我只是按照教程教给我的内容:
http://docs.python.org/py3k/tutorial/introduction.html#lists

4 个答案:

答案 0 :(得分:8)

你想要del

del time[0]

答案 1 :(得分:4)

为了完整性:

time.remove(15)

但在这种情况下,我会使用:

del time[0]

答案 2 :(得分:1)

实际上,如果您想按照示例所示进行操作,那么您将会这样做

time[0:1] = []

答案 3 :(得分:0)

实际上有无数种方法可以做到这一点。

我更喜欢time = time[1:]