无法列出指定大小的列表 [英] Having trouble making a list of lists of a designated size

查看:44
本文介绍了无法列出指定大小的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试列出约5000个列表,并且列表一直在混乱。

现在,我只是这样做:

I am trying to make a list of lists of about 5000 lists and it keeps messing up.
right now I just do this:

array = [[]]*5000
for line in f2:
    a = line.split()
    grid = int(a[0])
    array[grid].append(a[1])

print Counter(array[0]).most_common(10)

问题是当我进行计数器操作时,好像整个列表数组实际上只是一个列表一样。我做错了什么吗?
谢谢

the problem is when I make the counter it does it as if the whole array of lists was actually just one list. Is there something obvious that I am doing wrong? Thanks

推荐答案

使用 [[]] * 5000 ,您正在为外部列表中的同一列表创建 5000 引用。因此,如果您修改任何列表,它将修改所有列表。

Using [[]]*5000, you are creating 5000 reference to the same list in your outer list. So, if you modify any list, it will modify all of them.

您可以获得类似的不同列表:

You can get different lists like this:

a = [[] for _ in xrange(5000)]

这篇关于无法列出指定大小的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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