简体   繁体   English

在python中填写列表

[英]Filling up a list in python

I am trying to fill up a list of strings called lines with values from a dictionary called A in python.A is filled with integers.The pseudocode should be something like this.我试图用python中名为A的字典中的值填充一个名为lines的字符串列表。A填充有整数。伪代码应该是这样的。

 for i in xrange(0,256):
    for j in xrange(0,768):
        lines[j+4+(512*3)*i]= str(A[i,j])

As you can see I cannot insert the elements serially in lines.Is this possible?如您所见,我无法按行顺序插入元素。这可能吗? If it is then how should I do this?如果是,那么我该怎么做? The list lines would be pretty large of size 786435. It depends on both i an jI would be greatly obliged if anyone can help me.BTW I am using python 2.7 .列表行将非常大,大小为 786435。这取决于我和 jI,如果有人可以帮助我,我将非常感激。顺便说一句,我正在使用 python 2.7 。

Lists are contiguous sequences of elements.列表是元素的连续序列。 You could create the list up front, storing a reference to None at each index:您可以预先创建列表,在每个索引处存储对None的引用:

lines = [None] * (255 + 4 + (512 * 3) * 767)

but then you'd be creating an object with 1,178,371 (1 million plus) elements in it.但是随后您将创建一个包含 1,178,371(一百万多)个元素的对象。 That'll take a fair amount of memory just for the list object :这将需要相当数量的内存仅用于列表对象

>>> import sys
>>> sys.getsizeof([None] * 1178371)
9427040

That's 9MB for the list object alone, where each reference points to the same None object (it is a singleton, there is only ever one in memory).仅列表对象就有 9MB,其中每个引用都指向同一个None对象(它是一个单例,内存中只有一个)。 Add strings, and memory requirements rapidly climb;添加字符串,内存需求快速攀升; that many empty strings requires 40+ MB:许多字符串需要 40+ MB:

>>> sys.getsizeof('') * 1178371
43599727

In Python 2, every additional character will add a byte, so add average string length times 1MB to the requirements for just this list .在 Python 2 中,每增加一个字符都会增加一个字节,因此将平均字符串长度乘以 1MB 添加到仅此列表的要求 If your strings are only 3 characters, that's still doable;如果您的字符串只有 3 个字符,那仍然可行; that'll be about 50MB in total, but if you are talking about 5000 character strings you easily end up in the gigabytes.总共大约 50MB,但如果您谈论的是 5000 个字符串,您很容易最终达到千兆字节。 5000 characters isn't all that much. 5000 个字符并不是那么多。

Your indices are not contiguous, however.但是,您的索引不是连续的。 You only are producing 255 times 767 is 195585 indices;您只生成 255 次 767 是 195585 个索引; you appear to have a sparse data structure.你似乎有一个稀疏的数据结构。 You'd be better off using a dictionary in that case:在这种情况下,您最好使用字典:

lines = {}
for i in xrange(0,256):
    for j in xrange(0,768):
        lines[j + 4 + (512 * 3) * i]= str(A[i, j])

This works as written, because now each index is a key in a dictionary instead.这按书面方式工作,因为现在每个索引都是字典中的一个键。

If you do have to produce a continuous sequence, consider different techniques.如果您必须生成连续序列,请考虑不同的技术。 If all your indices, from 0 through to the very last, can be computed like the above, you could produce each value by calculating ;如果您的所有索引,从 0 到最后一个,都可以像上面那样计算,您可以通过计算来生成每个值; given an index, calculate what string would be placed at that index.给定一个索引,计算将放置在该索引处的字符串。 That way you don't have to produce all values up front.这样您就不必预先生成所有值。

The above loop indices follow a specific pattern, so you can simply derive the i and j from a given index:上面的循环索引遵循特定的模式,因此您可以简单地从给定的索引中导出ij

def str_for_index(index):
    i = index // (512 * 3)
    j = (index % (512 * 3)) - 4
    if 0 <= i < 256 and 0 <= j < 768:
        return str(A[i, j])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM