开发者

List updates in two places

开发者 https://www.devze.com 2023-02-02 04:19 出处:网络
In python you can create a list like so: [[0,0]]*n This creates a list such as this: [[0, 0], [0, 0], [0, 0]]

In python you can create a list like so:

[[0,0]]*n

This creates a list such as this:

[[0, 0], [0, 0], [0, 0]]

The issue is when you update the list such as:

li[0][0]=10
[[10, 0], [10, 0]开发者_如何学Python, [10, 0]]

Is there anyway to create lists of a certain size with this method but not encountering this problem?

I created this workaround but is there a better way?

for x in range(players):
    li+=[[0]*n]


li = [[0,0] for i in range(players)]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号