从条件列表中随机选择 - Python

时间:2015-11-14 21:15:56

标签: python list random cartesian-product

我在列表中创建了一个笛卡尔积,现在我想随机取出4个不同的元组(我在帖子的末尾尝试)。

shape = ['triangle' , 'square' , 'circle' , 'cross']
color = ['green' , 'red' , 'blue' , 'pink']

__cartesianPsc__ = list(itertools.product(shape , color))

我的笛卡儿产品

[('triangle', 'green'), ('triangle', 'red'), ('triangle', 'blue'), ('triangle', 'pink'), ('square', 'green'), ('square', 'red'), ('square', 'blue'), ('square', 'pink'), ('circle', 'green'), ('circle', 'red'), ('circle', 'blue'), ('circle', 'pink'), ('cross', 'green'), ('cross', 'red'), ('cross', 'blue'), ('cross', 'pink')]

现在我想随机制作4个不同的元组,包括三角形,正方形,圆形和十字形以及4种不同颜色的绿色,红色,粉红色,蓝色,但每种颜色和每种形状/颜色只有一次存在 例如:

首选

first = ('triangle', 'green')
second =  ('square', 'red')
third = ('circle', 'blue')
fourth = ('cross', 'pink')

第二

first = ('circle', 'blue')
second = ('cross', 'red') 
third = ('triangle', 'pink')
fourth = ('square', 'green')

有人知道怎么做吗?我尝试使用random.range的While-Loop从三角形,正方形,圆形,十字形中选择一个但我不知道如何做到这一点来获得不同的颜色

--------我的旧代码----- 我有一个问题,它总是有(三角形,绿色)作为一个元组,但其他3个元组是不同的(每次)。

shape = ['triangle' , 'square' , 'circle' , 'cross']
color = ['green' , 'red' , 'blue' , 'pink']

__cartesianPsc__ = list(itertools.product(shape , color))


while True:
    se1 = random.randrange(0, 4, 1)
    se2 = random.randrange(5, 8, 1)
    se3 = random.randrange(9, 12, 1)
    se4 = random.randrange(13, 16, 1)


# safe the randomly choosen tuple of the cartesian product
    first = __cartesianPsc__[se1] #triangle X color
    second = __cartesianPsc__[se2] # square X color
    third = __cartesianPsc__[se3] #circle X color
    fourth = __cartesianPsc__[se4] #cross X color


    # if statement to stop the While-LOOP only if there are 4 tuples with
    # 4 different shapes and colors !
    """Problem: (triangle, green) is always a tuple, no other color with triangle
    if  second[1] != first[1] and second[1] != third[1] and second[1] != fourth[1] \
    and third[1] != first[1] and third[1] != second[1] and third[1] != fourth[1] \
    and fourth[1] != first[1] and fourth[1] != second[1] and fourth[1] != third[1] \
    and first[1] != second[1] and first[1] != third[1] and first[1] != fourth[1]:
    """
 break

问候马丁:)

2 个答案:

答案 0 :(得分:2)

这应该做:

import random
def get4Random():
    shape = ['triangle' , 'square' , 'circle' , 'cross']
    color = ['green' , 'red' , 'blue' , 'pink']
    random.shuffle(shape)
    random.shuffle(color)
    return zip(shape, color)

随机随机播放每个列表,然后压缩随机值。

答案 1 :(得分:2)

扩展我的评论:根本不要创建笛卡尔积。只是改变形状和颜色,然后zip。在列表的第一个位置,我不得不做几次以获得与'triangle'不同的形状,但这只是随机的。

>>> import random
>>> shape = ['triangle' , 'square' , 'circle' , 'cross']
>>> color = ['green' , 'red' , 'blue' , 'pink']
>>> random.shuffle(shape)
>>> random.shuffle(color)
>>> list(zip(shape, color))
[('triangle', 'green'), ('cross', 'pink'), ('circle', 'red'), ('square', 'blue')]
>>> random.shuffle(color)
>>> random.shuffle(shape)
>>> list(zip(shape, color))
[('triangle', 'red'), ('cross', 'blue'), ('square', 'green'), ('circle', 'pink')]
>>> random.shuffle(color)
>>> random.shuffle(shape)
>>> list(zip(shape, color))
[('triangle', 'blue'), ('circle', 'green'), ('cross', 'red'), ('square', 'pink')]
>>> random.shuffle(shape)
>>> random.shuffle(color)
>>> list(zip(shape, color))
[('triangle', 'pink'), ('cross', 'green'), ('square', 'red'), ('circle', 'blue')]
>>> random.shuffle(shape)
>>> random.shuffle(color)
>>> list(zip(shape, color))
[('triangle', 'green'), ('square', 'pink'), ('cross', 'red'), ('circle', 'blue')]
>>> random.shuffle(shape)
>>> random.shuffle(color)
>>> list(zip(shape, color))
[('cross', 'green'), ('square', 'red'), ('circle', 'blue'), ('triangle', 'pink')]
相关问题