Python 如何检查一个阵列是否有按升序排列的卡,或两张相同的卡或三张相同的卡等

Python 如何检查一个阵列是否有按升序排列的卡,或两张相同的卡或三张相同的卡等,python,arrays,duplicates,Python,Arrays,Duplicates,我正在做一个扑克模拟,当时我被困在如何让python检查一个数组中的对、直、三种等等。我已经编写了我的代码,以便每个卡数组生成一个第二个数组,其中包含每张卡的存储值。 比如说 a=[1,3,4,2,5,8,1] b=[2,2,4,7,10] c=[5,6,5,5,5] 如何检查a是否至少有5个连续的数字(a直线),b是否至少有2个相互相等的数字(一对),c是否有4个。对手进行排序。这使得检查直线很容易,因为您需要按顺序排列五个数字 对于N类,遍历列表,查看每个项目的数量。例如: for po

我正在做一个扑克模拟,当时我被困在如何让python检查一个数组中的对、直、三种等等。我已经编写了我的代码,以便每个卡数组生成一个第二个数组,其中包含每张卡的存储值。 比如说

a=[1,3,4,2,5,8,1]  b=[2,2,4,7,10] c=[5,6,5,5,5]

如何检查a是否至少有5个连续的数字(a直线),b是否至少有2个相互相等的数字(一对),c是否有4个。对手进行排序。这使得检查直线很容易,因为您需要按顺序排列五个数字

对于N类,遍历列表,查看每个项目的数量。例如:

for pos in range(len(hand)):
    card_count = hand.count(hand[pos])
    if card_count >= 2:
        print "Hand has", card_count, hand[pos], "'s"

我并没有在这里透露所有的细节——这将为每一双打印两次,3种打印三次,等等。我假设您要求的是您需要的基本列表方法。

对手进行排序。这使得检查直线很容易,因为您需要按顺序排列五个数字

对于N类,遍历列表,查看每个项目的数量。例如:

for pos in range(len(hand)):
    card_count = hand.count(hand[pos])
    if card_count >= 2:
        print "Hand has", card_count, hand[pos], "'s"

我并没有在这里透露所有的细节——这将为每一双打印两次,3种打印三次,等等。我假设您要求的是您需要的基本列表方法。

对手进行排序。这使得检查直线很容易,因为您需要按顺序排列五个数字

对于N类,遍历列表,查看每个项目的数量。例如:

for pos in range(len(hand)):
    card_count = hand.count(hand[pos])
    if card_count >= 2:
        print "Hand has", card_count, hand[pos], "'s"

我并没有在这里透露所有的细节——这将为每一双打印两次,3种打印三次,等等。我假设您要求的是您需要的基本列表方法。

对手进行排序。这使得检查直线很容易,因为您需要按顺序排列五个数字

对于N类,遍历列表,查看每个项目的数量。例如:

for pos in range(len(hand)):
    card_count = hand.count(hand[pos])
    if card_count >= 2:
        print "Hand has", card_count, hand[pos], "'s"

我并没有在这里透露所有的细节——每双打印两次,每种打印三次,等等。我想您要求的是最基本的列表方法。

这应该足以让您开始

def check_hand(_hand_):
    last_c = ''
    straight = 0
    # This is illustrative, you can use this to return
    # the greatest hand one could have
    for card_value, number_of_occurences in _hand_.iteritems():
        if number_of_occurences == 2:
            print("We have a 2 of a kind")
        elif number_of_occurences == 3:
            print("We have a 3 of a kind")
        elif number_of_occurences == 4:
            print("We have a 4 of a kind")

        if last_c == '':
            last_c = card_value
        else:
            if card_value - last_c == 1:
                straight += 1
            last_c = card_value

    if straight >= 4:
        print("we have a straight")

a = [1, 3, 4, 2, 5, 8, 1]
b = [2, 2, 4, 7, 10]
c = [5, 6, 5, 5, 5]

# nifty way of assigning a dictionary with how many
# occurrences of a number in a list, and sorting it
check = {
    'a': dict((i, a.count(i)) for i in sorted(a)),
    'b': dict((i, b.count(i)) for i in sorted(b)),
    'c': dict((i, c.count(i)) for i in sorted(c)),
}

for which, hand in check.iteritems():
    print "Hand: " + which
    check_hand(hand)

这应该足以让你开始

def check_hand(_hand_):
    last_c = ''
    straight = 0
    # This is illustrative, you can use this to return
    # the greatest hand one could have
    for card_value, number_of_occurences in _hand_.iteritems():
        if number_of_occurences == 2:
            print("We have a 2 of a kind")
        elif number_of_occurences == 3:
            print("We have a 3 of a kind")
        elif number_of_occurences == 4:
            print("We have a 4 of a kind")

        if last_c == '':
            last_c = card_value
        else:
            if card_value - last_c == 1:
                straight += 1
            last_c = card_value

    if straight >= 4:
        print("we have a straight")

a = [1, 3, 4, 2, 5, 8, 1]
b = [2, 2, 4, 7, 10]
c = [5, 6, 5, 5, 5]

# nifty way of assigning a dictionary with how many
# occurrences of a number in a list, and sorting it
check = {
    'a': dict((i, a.count(i)) for i in sorted(a)),
    'b': dict((i, b.count(i)) for i in sorted(b)),
    'c': dict((i, c.count(i)) for i in sorted(c)),
}

for which, hand in check.iteritems():
    print "Hand: " + which
    check_hand(hand)

这应该足以让你开始

def check_hand(_hand_):
    last_c = ''
    straight = 0
    # This is illustrative, you can use this to return
    # the greatest hand one could have
    for card_value, number_of_occurences in _hand_.iteritems():
        if number_of_occurences == 2:
            print("We have a 2 of a kind")
        elif number_of_occurences == 3:
            print("We have a 3 of a kind")
        elif number_of_occurences == 4:
            print("We have a 4 of a kind")

        if last_c == '':
            last_c = card_value
        else:
            if card_value - last_c == 1:
                straight += 1
            last_c = card_value

    if straight >= 4:
        print("we have a straight")

a = [1, 3, 4, 2, 5, 8, 1]
b = [2, 2, 4, 7, 10]
c = [5, 6, 5, 5, 5]

# nifty way of assigning a dictionary with how many
# occurrences of a number in a list, and sorting it
check = {
    'a': dict((i, a.count(i)) for i in sorted(a)),
    'b': dict((i, b.count(i)) for i in sorted(b)),
    'c': dict((i, c.count(i)) for i in sorted(c)),
}

for which, hand in check.iteritems():
    print "Hand: " + which
    check_hand(hand)

这应该足以让你开始

def check_hand(_hand_):
    last_c = ''
    straight = 0
    # This is illustrative, you can use this to return
    # the greatest hand one could have
    for card_value, number_of_occurences in _hand_.iteritems():
        if number_of_occurences == 2:
            print("We have a 2 of a kind")
        elif number_of_occurences == 3:
            print("We have a 3 of a kind")
        elif number_of_occurences == 4:
            print("We have a 4 of a kind")

        if last_c == '':
            last_c = card_value
        else:
            if card_value - last_c == 1:
                straight += 1
            last_c = card_value

    if straight >= 4:
        print("we have a straight")

a = [1, 3, 4, 2, 5, 8, 1]
b = [2, 2, 4, 7, 10]
c = [5, 6, 5, 5, 5]

# nifty way of assigning a dictionary with how many
# occurrences of a number in a list, and sorting it
check = {
    'a': dict((i, a.count(i)) for i in sorted(a)),
    'b': dict((i, b.count(i)) for i in sorted(b)),
    'c': dict((i, c.count(i)) for i in sorted(c)),
}

for which, hand in check.iteritems():
    print "Hand: " + which
    check_hand(hand)

请展示你迄今为止的尝试请展示你迄今为止的尝试请展示你迄今为止的尝试请展示你迄今为止的尝试