删除整个数组Python
如何擦除整个数组,使其不包含任何项目?
我想这样做,这样我就可以在其中存储新值(一组新的 100 个浮点数)并找到最小值。
现在,我的程序正在读取我认为之前的集合中的最小值,因为它正在将其自身附加到仍在那里的前一个集合中。顺便说一句,我使用 .append 。
How do I erase a whole array, leaving it with no items?
I want to do this so I can store new values in it (a new set of 100 floats) and find the minimum.
Right now my program is reading the minimum from sets before I think because it is appending itself with the previous set still in there. I use .append by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
很简单:
将
array
设置为空列表。 (顺便说一句,它们在 Python 中称为列表,而不是数组)如果这对您不起作用,请编辑您的问题以包含演示您的问题的代码示例。
It's simple:
will set
array
to be an empty list. (They're called lists in Python, by the way, not arrays)If that doesn't work for you, edit your question to include a code sample that demonstrates your problem.
是的,数组确实存在,不,当涉及到诸如
del
和append
之类的事情时,它们与列表没有什么不同:值得注意的差异:您需要在以下情况下指定类型:创建数组,并且在执行
arr[i] = expression
或arr.append(expression)< 时,可以节省存储空间,但需要在 C 类型和 Python 类型之间进行转换的额外时间/code> 和
左值 = arr[i]
Well yes arrays do exist, and no they're not different to lists when it comes to things like
del
andappend
:Differences worth noting: you need to specify the type when creating the array, and you save storage at the expense of extra time converting between the C type and the Python type when you do
arr[i] = expression
orarr.append(expression)
, andlvalue = arr[i]
现在回答您可能应该问的问题,例如“我从某处得到 100 个浮点数;在找到最小值之前我是否需要将它们放入数组或列表中?”
答案:不,如果
somewhere
是可迭代的,则可以这样做:
示例:
Now to answer the question that perhaps you should have asked, like "I'm getting 100 floats form somewhere; do I need to put them in an array or list before I find the minimum?"
Answer: No, if
somewhere
is a iterable, instead of doing this:you can do this:
Example:
Python列表clear()方法
clear() 方法从列表中删除所有元素。
Python List clear() Method
The clear() method removes all the elements from a list.
请注意
list
和array
是不同的班级。您可以这样做:这实际上会修改您现有的列表。大卫的答案创建了一个新列表并将其分配给同一个变量。您想要哪个取决于具体情况(例如,是否有任何其他变量引用同一列表?)。
尝试:
每次打印
a
和b
以查看差异。Note that
list
andarray
are different classes. You can do:This will actually modify your existing list. David's answer creates a new list and assigns it to the same variable. Which you want depends on the situation (e.g. does any other variable have a reference to the same list?).
Try:
and
Print
a
andb
each time to see the difference.