如何在Python中创建一个整数数组? [英] How to create an integer array in Python?

查看:3972
本文介绍了如何在Python中创建一个整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不应该是这么难。我的意思是在C,

It should not be so hard. I mean in C,

int a[10];

是你所需要的。如何创建全零的一个随机大小的阵列。我知道numpy的零点()函数,但必须有一个简单的方法内置,不是另一个模块。

is all you need. How to create an array of all zeros for a random size. I know the zeros() function in NumPy but there must be an easy way built-in, not another module.

推荐答案

如果你不满意名单(因为它们可以包含任何东西,占用过多的内存),你可以使用整数数组高效:

If you are not satisfied with lists (because they can contain anything and take up too much memory) you can use efficient array of integers:

array.array('i')

请参阅 href=\"http://docs.python.org/library/array.html\">

See here

如果你需要对它进行初始化,

If you need to initialize it,

a=array.array('i',(0 for i in range(0,10)))

这篇关于如何在Python中创建一个整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆