1

我需要在随机位置填充几个 1 的列表。我可以成功地创建一个随机数列表:

from random import randint
l = [randint(0,1023) for _ in range(0,10)]

如何在 l 指定的位置填充 1 的列表?

4

4 回答 4

8

我需要在随机位置创建一个包含 10 - 40 l 的 0 的大列表来对算法进行基准测试。

这对你有用吗?

import random

zeros = [0] * 1024
ones = [1] * random.randint(10, 40)
l =  zeros + ones
random.shuffle(l)

# the list l contains many zeros and 10 - 40 1's in random places.

where_the_ones_are = [i for i, x in enumerate(l) if x == 1] 
于 2013-07-08T09:26:28.120 回答
7

稀疏列表

我对“稀疏列表”的理解是,大多数(例如,超过 95% 的)值将为零,并且出于内存效率的原因,您不希望存储这些值(参见 稀疏数组)。

列表理解

使用您的列表理解,您可以使用条件表达式解析( foo if 条件 else ) 来确定该位置是 1 还是 0。例如:

In [1]: from random import randint

In [2]: l = [randint(0,1023) for _ in range(0,10)]

In [3]: l
Out[3]: [987, 356, 995, 192, 21, 22, 1013, 375, 796, 339]

In [4]: 1 if 987 in l else 0
Out[4]: 1

In [5]: 1 if 988 in l else 0
Out[5]: 0

这意味着您不需要填充您在问题中提到的第二个列表,您只需遍历 0 - 1023 范围并使用:

1 if index in l else 0

字典理解

或者,您可以使用字典理解。我认为这更具可读性:

In [1]: from random import randint
In [2]: l = {randint(0, 1023): 1 for _ in xrange(0, 10)}

这将生成一个像这样的字典:

In [3]: l
Out[3]: 
{216: 1,
 381: 1,
 384: 1,
 392: 1,
 396: 1,
 472: 1,
 585: 1,
 630: 1,
 784: 1,
 816: 1}

然后访问元素,指定默认值为零。如果设置了请求位置的值,您将得到一个:

In [4]: l.get(216, 0)
Out[4]: 1

如果未设置该值,您将得到一个零:

In [5]: l.get(217, 0)
Out[5]: 0

获取职位列表:

In [6]: l.keys()
Out[6]: [384, 392, 472, 630, 216, 585, 396, 381, 784, 816]

上述两种方法的缺陷

randint(0, 1023)可能会多次发出相同的数字,从而导致冲突,这将导致少于所需的数量。

将它们捆绑在一起

我会将基于字典的实现包装在 aclass中,以使其易于(重新)使用。

from random import randint


class RandomSparseList(object):
    def __init__(self, size, min_bits, max_bits):
        self.size = int(size)
        self.bits = {}
        self.bits_set = randint(min_bits, max_bits)
        while self.bits_set > len(self.bits):
            self.bits[randint(0, self.size)] = 1 

    def __len__(self):
        return self.size

    def __getitem__(self, index):
        if index < 0 or index >= self.size:
            raise IndexError
        return self.bits.get(int(index), 0)

    def __iter__(self):
        for i in xrange(self.size):
            yield self.__getitem__(i)

    def __contains__(self, index):
        return index in self.bits

    def __repr__(self):
        return '[{}]'.format(', '.join(str(x) for x in self))

    def set_bits(self):
        return self.bits.keys()

示例用法

我已经把它class放在一个文件中:

In [1]: from random_sparse_list import RandomSparseList

创建一个实例:

In [2]: rsl = RandomSparseList(1024, 10, 40)

检查列表的长度:

In [3]: len(rsl)
Out[3]: 1024

设置了哪些位?

In [4]: rsl.set_bits()
Out[4]: 
[523,
 400,
 285,
 158,
 419,
 434,
 701,
 67,
 843,
 846,
 591,
 720,
 470,
 864,
 912,
 739,
 996,
 485,
 489,
 234,
 1005,
 573,
 381,
 784]

24:肯定在 10-40 的范围内

随机访问:

In [5]: rsl[523]
Out[5]: 1

In [6]: rsl[524]
Out[6]: 0

是不是有点设置?

In [7]: 400 in rsl
Out[7]: True

In [8]: 401 in rsl
Out[8]: False

迭代列表:

In [9]: for index, value in enumerate(rsl):
   ...:     if value:
   ...:         print '{} found at index {}'.format(value, index)
   ...:         
1 found at index 67
1 found at index 158
1 found at index 234
1 found at index 285
1 found at index 381
1 found at index 400
1 found at index 419
1 found at index 434
1 found at index 470
1 found at index 485
1 found at index 489
1 found at index 523
1 found at index 573
1 found at index 591
1 found at index 701
1 found at index 720
1 found at index 739
1 found at index 784
1 found at index 843
1 found at index 846
1 found at index 864
1 found at index 912
1 found at index 996
1 found at index 1005

字符串表示:

In [10]: rsl
Out[10]: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

笔记

基于-set的实现将更加节省内存,但上面的dict0可以很容易地更改为包含除and之外的(随机或其他)值1

更新

受这个问题和缺乏标准稀疏list实现的启发,我在 Cheese Shop 中添加了一个 sparse_list实现。您可以安装它,pip install sparse_list然后RandomSparseList实现对您来说更简单:

from sparse_list import SparseList
from random import randint


class RandomSparseList(SparseList):
    def __init__(self, size, min_bits, max_bits):
        super(RandomSparseList, self).__init__(size, 0)
        self.bits = randint(min_bits, max_bits)
        while self.bits > len(self.elements):
            self.elements[randint(0, self.size)] = 1

这将与上面的示例完全相同,但有一些额外的功能,例如扩展切片。您可以阅读(并贡献)GitHub 上的源代码。

于 2013-07-08T09:40:07.340 回答
1

您可以使用此代码段:

ones_lst = [SOME_VALUE] * 1024
for val in l:
  ones_lst[val] = 1

这里替换SOME_VALUE为您使用的不同值,除了一个(可能为零)

于 2013-07-08T09:10:23.263 回答
1

这是我的解决方案。位置和 1 的数量都是随机的。您将拥有一个数组中的位置来检查您的算法是否有效。

from random import randint

MAX_NUMS=1000
#big array of 0s
arr = [0] * MAX_NUMS

#How many 1s do you want?
numOnes=randint(10, 40)
ones=[1]*numOnes

#Fill array with random postions 
for i in range(0,numOnes-1):
        ones[i] =  randint(0,MAX_NUMS-1)
print ones

#Set arr to 1 for each random postion
for pos in ones:
        arr[pos]=1
print arr
于 2013-07-08T10:17:04.643 回答