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

Random selection from list with condition - Python

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

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 种不同的颜色绿色、红色、粉色、蓝色,但是每种颜色和每个 shape/color 都只有一次存在的 示例:

第一顺位

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 循环从三角形、正方形、圆形、十字形中选择一个,但我不知道如何获得不同的颜色

--------我的旧代码----- 我有一个问题,它总是有(三角形,绿色)作为一个元组,但其他 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

问候马丁 :)

应该这样做:

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

随机打乱每个列表,然后压缩打乱后的值。

扩展我的评论:根本不要创建笛卡尔积。只需将形状和颜色以及 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')]