删除整个数组Python

发布于 2024-09-14 07:16:34 字数 153 浏览 5 评论 0原文

如何擦除整个数组,使其不包含任何项目?

我想这样做,这样我就可以在其中存储新值(一组新的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

咆哮 2024-09-21 07:16:35

很简单:

array = []

array 设置为空列表。 (顺便说一句,它们在 Python 中称为列表,而不是数组)

如果这对您不起作用,请编辑您的问题以包含演示您的问题的代码示例。

It's simple:

array = []

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.

何处潇湘 2024-09-21 07:16:35

是的,数组确实存在,不,当涉及到诸如 delappend 之类的事情时,它们与列表没有什么不同:

>>> from array import array
>>> foo = array('i', range(5))
>>> foo
array('i', [0, 1, 2, 3, 4])
>>> del foo[:]
>>> foo
array('i')
>>> foo.append(42)
>>> foo
array('i', [42])
>>>

值得注意的差异:您需要在以下情况下指定类型:创建数组,并且在执行 arr[i] = expressionarr.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 and append:

>>> from array import array
>>> foo = array('i', range(5))
>>> foo
array('i', [0, 1, 2, 3, 4])
>>> del foo[:]
>>> foo
array('i')
>>> foo.append(42)
>>> foo
array('i', [42])
>>>

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 or arr.append(expression), and lvalue = arr[i]

你是暖光i 2024-09-21 07:16:35

现在回答您可能应该问的问题,例如“我从某处得到 100 个浮点数;在找到最小值之前我是否需要将它们放入数组或列表中?”

答案:不,如果 somewhere 是可迭代的,则

temp = []
for x in somewhere:
   temp.append(x)
answer = min(temp)

可以这样做:

answer = min(somewhere)

示例:

answer = min(float(line) for line in open('floats.txt'))

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:

temp = []
for x in somewhere:
   temp.append(x)
answer = min(temp)

you can do this:

answer = min(somewhere)

Example:

answer = min(float(line) for line in open('floats.txt'))
多彩岁月 2024-09-21 07:16:35

Python列表clear()方法
clear() 方法从列表中删除所有元素。

fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
print(fruits)
# outputs: []

Python List clear() Method
The clear() method removes all the elements from a list.

fruits = ['apple', 'banana', 'cherry', 'orange']
fruits.clear()
print(fruits)
# outputs: []
此岸叶落 2024-09-21 07:16:34

请注意 listarray是不同的班级。您可以这样做:

del mylist[:]

这实际上会修改您现有的列表。大卫的答案创建了一个新列表并将其分配给同一个变量。您想要哪个取决于具体情况(例如,是否有任何其他变量引用同一列表?)。

尝试:

a = [1,2]
b = a
a = []

每次打印

a = [1,2]
b = a
del a[:]

ab 以查看差异。

Note that list and array are different classes. You can do:

del mylist[:]

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:

a = [1,2]
b = a
a = []

and

a = [1,2]
b = a
del a[:]

Print a and b each time to see the difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文