1
kkwezard 2015 年 6 月 25 日 a = [0] * 100
|
2
hahastudio 2015 年 6 月 25 日 你要的是
[0] * 100 还是 [0 for i in range(100)] 呢 |
3
db253874 2015 年 6 月 25 日 a = [0,] * 100
|
4
Azul 2015 年 6 月 25 日 list(map(int, 100*'0')) 应该还有比这个好的吧
|
5
hjq98765 OP |
6
hjq98765 OP @hahastudio 第一种方法看起来简单些
|
7
hahastudio 2015 年 6 月 25 日 |
8
hahastudio 2015 年 6 月 25 日
|
9
aiver 2015 年 6 月 25 日
如@hahastudio说的,如果直接用[0]*100的做法有坑,推荐使用列表推导式构建[0 for i in range(100)]
|
10
lilydjwg 2015 年 6 月 25 日
|
11
weyou 2015 年 6 月 25 日
就原题来讲,当然是a = [0] * 100最简单。
至于坑,那是另外的情况了。 |
12
zerh925 2015 年 6 月 26 日
import numpy as np
a = np.zeros((1, 100)).tolist()[0] just for fun |
13
firemiles 2015 年 6 月 26 日
@hahastudio 这个有什么问题吗,我在python2.7.9下运行是一样的,难道在3下改变了吗?
|
14
firemiles 2015 年 6 月 26 日
@hahastudio 刚想明白了[[1,2]]*100产生的100个list指向相同的对象,修改其中一个时会改变其他99个
|
16
aheadlead 2015 年 6 月 26 日
@Yiph 至少python2下面是这样的
>>> class foo: ... pass ... >>> s = foo() >>> s.a = 233 >>> a = [s]*10 >>> a[0].a 233 >>> a[1].a 233 >>> a[2].a 233 >>> a[0], a[1], ..., a[9] 都指向了同一个对象 >>> for item in a: ... print id(a) ... 4522747664 4522747664 4522747664 4522747664 4522747664 4522747664 4522747664 4522747664 4522747664 4522747664 >>> |