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

2 投票
4 回答
6268 浏览
提问于 2025-04-16 11:25
# 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 个回答

1

其实,如果你想按照这个例子来做,你需要这样做:

time[0:1] = []
4

为了完整性:

time.remove(15)

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

del time[0]
8

你需要用到del这个命令。

del time[0]

撰写回答