从两个列表中采样随机元素,其中value = 1

时间:2017-10-30 19:54:15

标签: python list

我有两个列表cluster0YclusterY,它们由1和0组成。例如

cluster0Y = [0,1,0,0,1]
cluster1Y = [0,0,0,1]

我想从列表cluster0Y和cluster1Y中随机抽取值为1的1个元素。然后我想打印它所属的列表并打印索引。为此,我编写了以下代码:

from random import randrange
cluster0Y = [0,1,0,1]
cluster1Y = [0,1,0,1]
while True:
        random_index = randrange(0,len(cluster0Y+cluster1Y))
        print(str(random_index))
        if random_index > len(cluster0Y):
            random_index = random_index - len(cluster0Y)
            if cluster1Y[random_index]==1:
                print('cluster 1 ' + str(random_index))
                break
        else:
            if cluster0Y[random_index]==1:
                print('cluster 0 ' + str(random_index))
                break
        print(str(random_index))

但是,此代码有时会打印出列表中不为1的值。为什么会这样?

4 个答案:

答案 0 :(得分:1)

您肯定选择值不是1但不打印它们。您正在打印指向非1值的随机索引。

您还可以重新考虑您的逻辑,使其看起来更像

extract the indices of 1 values in list0
extract the indices of 1 values in list1
pick a random option from the extracted indices

有些事情:

allOnes0 = [(ind, 0) for ind, v in enumerate(cluster0Y) if v == 1]
allOnes1 = [(ind, 1) for ind, v in enumerate(cluster1Y) if v == 1]
(index, clusterId) = random.choice(allOnes0 + allOnes1)

clusterId告诉您随机1是来自群集0还是1,index告诉您群集中1的位置。

哪个更直接,更不容易出错。

答案 1 :(得分:0)

在此处按预期工作,但您必须修复一个错误:

/**
 * BMP-related data types based on Microsoft's own.
 */

#include <stdint.h>

/**
 * Common Data Types 
 *
 * The data types in this section are essentially aliases for C/C++ 
 * primitive data types.
 *
 * Adapted from https://msdn.microsoft.com/en-us/library/cc230309.aspx.
 * See http://en.wikipedia.org/wiki/Stdint.h for more on stdint.h.
 */
typedef uint8_t  BYTE;
typedef uint32_t DWORD;
typedef int32_t  LONG;
typedef uint16_t WORD;

/**
 * BITMAPFILEHEADER
 *
 * The BITMAPFILEHEADER structure contains information about the type, size,
 * and layout of a file that contains a DIB [device-independent bitmap].
 *
 * Adapted from https://msdn.microsoft.com/en-us/library/dd183374(v=vs.85).aspx.
 */
typedef struct 
{ 
    WORD bfType; 
    DWORD bfSize; 
    WORD bfReserved1; 
    WORD bfReserved2; 
    DWORD bfOffBits; 
} __attribute__((__packed__)) 
BITMAPFILEHEADER; 

/**
 * BITMAPINFOHEADER
 *
 * The BITMAPINFOHEADER structure contains information about the 
 * dimensions and color format of a DIB [device-independent bitmap].
 *
 * Adapted from https://msdn.microsoft.com/en-us/library/dd183376(v=vs.85).aspx.
 */
typedef struct
{
    DWORD biSize; 
    LONG biWidth; 
    LONG biHeight; 
    WORD biPlanes; 
    WORD biBitCount; 
    DWORD biCompression; 
    DWORD biSizeImage; 
    LONG biXPelsPerMeter; 
    LONG biYPelsPerMeter; 
    DWORD biClrUsed; 
    DWORD biClrImportant; 
} __attribute__((__packed__))
BITMAPINFOHEADER; 

/**
 * RGBTRIPLE
 *
 * This structure describes a color consisting of relative intensities of
 * red, green, and blue.
 *
 * Adapted from https://msdn.microsoft.com/en-us/library/dd162939(v=vs.85).aspx.
 */
typedef struct
{
    BYTE rgbtBlue;
    BYTE rgbtGreen;
    BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;

..如果random_index的大小正好为len(cluster0Y),则无法在cluster0Y中读取。

if random_index > len(cluster0Y):

答案 2 :(得分:0)

我认为你的代码对于你想要实现的目标来说太复杂了。

您可以使用类似[idx for idx, val in enumerate(cluster) if val != 0]的内容来查找非零元素的索引并从那里开始。

至于为什么它有时打印0s,我认为@MatsLindh找到了原因。

答案 3 :(得分:0)

我稍微修改了你的代码。我将其更改为>=,因为在random_index = 4时根据您的代码,它不会满足条件random_index > len(cluster0Y),并且您将获得超出范围错误的列表

from random import randrange
cluster0Y = [0,1,0,1]
cluster1Y = [0,1,0,1]
while True:
        random_index = randrange(0,len(cluster0Y+cluster1Y))
        if random_index >= len(cluster0Y):
            random_index = random_index - len(cluster0Y)
        if cluster1Y[random_index]==1:
            print('cluster 1 : ',random_index)
            break
        if cluster0Y[random_index]==1:
            print('cluster 0 : ',random_index)
            break
        print('random index = ',random_index)