我都是
a=range(1,100)
b=range(1,100)
for i in a:
b[i]=b[i]-i
这样太笨了,有好的方法没?
a=range(1,100)
b=range(1,100)
for i in a:
b[i]=b[i]-i
这样太笨了,有好的方法没?
|
1
a = [0] * 100
|
|
2
你要的是
[0] * 100 还是 [0 for i in range(100)] 呢 |
|
3
a = [0,] * 100
|
|
4
list(map(int, 100*'0')) 应该还有比这个好的吧
|
|
5
|
|
6
@hahastudio 第一种方法看起来简单些
|
|
7
|
|
hahastudioJun 25, 2015
8
|
|
aiverJun 25, 2015
9
如@hahastudio说的,如果直接用[0]*100的做法有坑,推荐使用列表推导式构建[0 for i in range(100)]
|
|
lilydjwgJun 25, 2015
10
|
|
weyouJun 25, 2015
11
就原题来讲,当然是a = [0] * 100最简单。
至于坑,那是另外的情况了。 |
|
zerh925Jun 26, 2015
12
import numpy as np
a = np.zeros((1, 100)).tolist()[0] just for fun |
|
firemilesJun 26, 2015
13
@hahastudio 这个有什么问题吗,我在python2.7.9下运行是一样的,难道在3下改变了吗?
|
|
firemilesJun 26, 2015
14
@hahastudio 刚想明白了[[1,2]]*100产生的100个list指向相同的对象,修改其中一个时会改变其他99个
|
|
aheadleadJun 26, 2015
16
@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 >>> |