Example: I want to select tuple in an array(t_bet), which contains just one element from judge_tpl = (5,12,24)
. For example, (3, 4, 5, 16, 30)
is ok while (3, 12, 24, 29, 30)
is not because it contains two elements.
t_bet = [(3, 12, 24, 29, 30),
(3, 13, 18, 19, 22),
(1, 11, 16, 17, 22),
(3, 4, 5, 16, 30),
(1, 11, 15, 20, 24)]
lst = []
for i in t_bet:
if (5 in i) and (12 not in i) and (24 not in i ) :
lst.append(i)
elif (5 not in i) and (12 in i) and (24 not in i ):
lst.append(i)
elif (5 not in i) and (12 not in i) and (24 in i ):
lst.append(i)
lst: [(3, 4, 5, 16, 30), (1, 11, 15, 20, 24)]
I have to list all combinations using if..elif
,which is inconvenient especially when there are so many elements in judge_tpl
.
Notice: The tuples in t_bet
have same lengths while there are more than 10 elements in judge_tpl
.
[bet for bet in t_bet if len(set(bet) & set(judge_tpl)) == 1]
\$\endgroup\$