2015-11-14 60 views
2

我在列表中創建了一個笛卡爾積,現在我想隨機取出4個不同的元組(我試着在帖子結尾處)。從條件列表中隨機選擇 - Python

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從三角形,正方形,圓形,十字形,但我不知道如何做到這一點,以獲得不同的顏色

--------我的舊Code- ---- 我有問題,它總是(三角形,綠色)作爲元組,但其他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 

問候馬丁:)

+0

請勿先創建笛卡爾產品。只需單獨洗刷形狀和顏色,然後「壓縮」它們。 – Jasper

+0

是否有你爲'__cartesianPsc__'使用雙下劃線的原因?通常這種下劃線用於表示魔術物體。 PEP8說:「不要發明這樣的名字,只用它們作爲記錄」 – jme

回答

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')] 
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) 

隨機混合每個列表,然後壓縮洗牌後的值。