Get the latest news, exclusives, sport, celebrities, showbiz, politics, business and lifestyle from The VeryTime,Stay informed and read the latest news today from The VeryTime, the definitive source.

How to Find Subsets in a Python

41
    • 1). Import the "set" module to access set functions:

      >>>from sets import Set

    • 2). Create two sets. The first set, "x," will represent a larger set of data. The second set, "y," will represent a smaller set that you can compare to x in order to determine if it is a subset of x. The set y is considered a subset of x if all elements in y are also in x:

      >>>x = Set([1, 4, 5, 6, 4, 3, 5, 7, 8, 2, 3])
      >>>y = Set([1, 2, 3])

    • 3). Determine if y is a subset of x with the "issubset" function built into all Python sets:

      >>>y.issubset(x)
      True

    • 4). Change y, adding a new element that does does not exist in x. Then check again to see if y is a subset of x:

      >>>y = Set([1, 2, 10])
      >>>y.issubset(x)
      False

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.