h1h1
h1h1

Reputation: 790

Python - Comparing lists

I have 3 lists, the first of which is a list of 5 random digits, as shown below:

import random

def rollDice():
    dice = []
    for i in range(5):
        dice.append(random.randint(1,6))
    return sorted(dice)

dice = rollDice()
largeStraight = [[1,2,3,4,5] , [2,3,4,5,6]]
smallStraight = [[1,2,3,4] , [2,3,4,5] , [3,4,5,6]]

My question is what is the best way to see if dice is equal to either of the nested lists in largeStraight, and secondly whether any of the nested lists in smallStraight are a subset of dice. I am looking for a simple true or false return.

Thanks for any help.

Upvotes: 0

Views: 149

Answers (3)

rlms
rlms

Reputation: 11060

If order is important, for the large straight, you could simply do this:

dice in largeStraight

and for the small straight, you could do this:

any(i in (dice[0:4], dice[1:5]) for i in smallStraight)

Alternatively, you could replace the rollDice function with this:

def rollDice():
    dice = set()
    for i in range(5):
        dice.add(random.randint(1, 6))
    return dice

and use sets as suggested in other answers.

You could also replace your current rollDice definition with a list comprehension:

def rollDice():
    return sorted([random.randint(1, 6) for _ in range(5)])

or

def rollDice():
    return {random.randint(1, 6) for _ in range(5)}

for a set.

However, I'd advise against using sets. It would work in this case, but I presume that this is part of a larger program. Sets can't have duplicate elements, so if you later wanted to check if there was a pair of equal numbers in dice, and it was a set, then you would always get a negative response.

Upvotes: 3

user1456308
user1456308

Reputation:

Here is possible solution

is_large_straight = any(set(x) & set(dice) == set(x) for x in largeStraight)
is_small_straight = any(set(x) & set(dice) == set(x) for x in smallStraight)

I hope it'll help you.

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1124828

Use sets instead of lists:

largeStraight = [{1, 2, 3, 4, 5}, {2, 3, 4, 5, 6}]
smallStraight = [{1, 2, 3, 4}, {2, 3, 4, 5} , {3 ,4, 5, 6}]

Now you can use set operations:

if any(ls.issubset(dice) for ls in largeStraight):
    # a large straight
elif any(ss.issubset(dice) for ss in smallStraight):
    # a small straight

You can still turn each list in largeStraight and smallStraight into a set in the generator expression passed to any(), but that'd be a waste of CPU cycles.

Demo:

>>> dice = [2, 3, 5, 1, 4]
>>> if any(ls.issubset(dice) for ls in largeStraight):
...     print 'Large!'
... elif any(ss.issubset(dice) for ss in smallStraight):
...     print 'Small!'
... 
Large!
>>> dice = [2, 3, 5, 1, 6]
>>> if any(ls.issubset(dice) for ls in largeStraight):
...     print 'Large!'
... elif any(ss.issubset(dice) for ss in smallStraight):
...     print 'Small!'
... 
>>> dice = [2, 3, 6, 4, 1]
>>> if any(ls.issubset(dice) for ls in largeStraight):
...     print 'Large!'
... elif any(ss.issubset(dice) for ss in smallStraight):
...     print 'Small!'
... 
Small!

Upvotes: 4

Related Questions