使用条件Python从列表中随机选择

2024-09-27 21:29:05 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

我的笛卡尔积

^{pr2}$

现在我想随机得到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')

等等

有人知道怎么做吗?我试过用随机范围从三角形、正方形、圆形、十字中挑一个,但我不知道怎么做才能得到不同的颜色

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

你好,马丁:)


Tags: andgreenredcolorfirst元组secondtriangle
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')]

这应该做到:

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

随机洗牌每个列表,然后压缩洗牌后的值。在

相关问题 更多 >

    热门问题