如何根据列表的输入让输出 return True 或 False?

How can I let the output return True or False based on the input of the list?

所以我有一个列表 hand,其数据是这样的 ['AS', 'AD', 'AC', 'CH', 'CS']。现在我必须找到列表中每个元素的 第一个字符 并查看是否有 3 个相同的字母 2 个相同字母与否.

注意它们必须全部按顺序排列......就像总是 x,x,x,y,y 或 y,y,x,x ,X。任何其他组合,如 x,y,y,x,x 或 x,y,x,y,x,将 return False

例如

['AS', 'AD', 'AC', 'CH', 'CS'] returns True因为有3个A和2个C.

['AS', 'SD', 'SC', 'CH', 'CS'] returns False因为顺序不对

['CS', 'CD', 'AC', 'AH', 'AS'] returns True因为有3个A和2个C

['AS', 'CD', 'DC', 'AH', 'CS'] returns False因为none个字符出现了3次和2次

到目前为止,这是我的代码,它不起作用...

hand = ['AS', 'AD', 'AC', 'CH', 'CS']
updated_hands = list([x[1] for x in hand])
if ((updated_hands[0] == updated_hands[1] == updated_hands[2]) or (updated_hands[2] == updated_hands[3] == updated_hands[4])) and ((updated_hands[3] == updated_hands[4]) or (updated_hands[0] == updated_hands[1])):
    print('True')
else:
    print('False')

我应该对此进行哪些更改?

这是我要采用的方法,它实际上与您尝试过的方法相同,但我只是对其进行了一些扩展以尝试使其更具可读性。我认为您所得到的可能是“正确的”,但是将它作为一个衬里来做的复杂性隐藏了一个小错误。

def test_list(hand):
    test1 = all(let[0] == hand[0][0] for let in hand[:3])
    test2 = all(let[0] == hand[0][0] for let in hand[:2])
    test3 = all(let[0] == hand[2][0] for let in hand[2:])
    test4 = all(let[0] == hand[3][0] for let in hand[3:])
    
    if (test1 and test3) or (test2 and test4):
        print("True")
    else:
        print("False")

hand1 = ['AS', 'AD', 'AC', 'CH', 'CS']
hand2 = ['AS', 'SD', 'SC', 'CH', 'CS']
hand3 = ['CS', 'CD', 'AC', 'AH', 'AS']
hand4 = ['AS', 'CD', 'DC', 'AH', 'CS']

test_list(hand1) #prints True
test_list(hand2) #prints False
test_list(hand3) #prints True
test_list(hand4) #prints True

下面是适用于任意长度和顺序的东西:

hands = [['AS', 'AD', 'AC', 'CH', 'CS']]
hands.append(['AS', 'SD', 'SC', 'CH', 'CS'])
hands.append(['CS', 'CD', 'AC', 'AH', 'AS'])
hands.append(['AS', 'CD', 'DC', 'AH', 'CS'])

for hand in hands:

    condition = True

    last_count = 1
    last_item = hand.pop(0)[0]

    while hand:
        item = hand.pop(0)

        if (item[0] == last_item):
            last_count += 1 

        if (last_item != item[0]):
            if (last_count == 2) or (last_count == 3):
                condition = True
            else:
                condition = False
                break

        last_item = item[0]

    print(condition)

输出:

True
False
True
False
    Lets Make it Simple.

    hand = ['AS', 'AD', 'AC', 'CH', 'CS']
    a = [sorted(han[0][0]) for han in hand] 
    
    import itertools
    b = itertools.chain(*a)
    c = list(b)
    
    d = [han[0][0] for han in hand] 
    
    if c == d:
    print(True)
    else:
    print(False)
=======================================================================
You Can Get Any List From user.

You Can Get List From User