Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Set Comprehension

So I have these two problems for a homework assignment and I'm stuck on the second one.

  1. Use a Python Set Comprehension (Python's equivalent of Set Builder notation) to generate a set of all of the prime numbers that are less than 100. Recall that a prime number is an integer that is greater than 1 and not divisible by any integer other than itself and 1. Store your set of primes in a variable (you will need it for additional parts). Output your set of primes (e.g., with the print function).

  2. Use a Python Set Comprehension to generate a set of ordered pairs (tuples of length 2) consisting of all of the prime pairs consisting of primes less than 100. A Prime Pair is a pair of consecutive odd numbers that are both prime. Store your set of Prime Pairs in a variable. Your set of number 1 will be very helpful. Output your Set of Prime Pairs.

For the first one, this works perfectly:

r= {x for x in range(2, 101)  if not any(x % y == 0 for y in range(2, x))}  

However, I'm pretty stumped on the second one. I think I may have to take the Cartesian product of the set r with something but I'm just not sure.

This gets me somewhat close but I just want the consecutive pairs.

cart = { (x, y) for x in r for y in r      if x < y } 
like image 400
user3308790 Avatar asked Feb 14 '14 04:02

user3308790


People also ask

Is there a set comprehension in Python?

What is set comprehension in Python? Set comprehension is a method for creating sets in python using the elements from other iterables like lists, sets, or tuples. Just like we use list comprehension to create lists, we can use set comprehension instead of for loop to create a new set and add elements to it.

Can you do list comprehension on a set?

A set comprehension is similar to a list comprehension but returns a set instead of a list. The syntax is slightly different in the sense that we use curly brackets instead of square brackets to create a set. The list includes a lot of duplicates, and there are names with only a single letter.

How do you create a set with comprehension?

A set comprehension carries the following steps: First, iterate over the elements of a set. Second, apply an expression to each element. Third, create a new set of elements resulting from the expression.

What is meant by set comprehension?

Set comprehension is a mathematical notation for defining sets on the basis of a property of their members. Although set comprehension is widely used in mathematics and some programming languages, direct support for reasoning about it is still not readily available in state-of-the-art SMT solvers.

What is comprehensions in Python?

Comprehensions in Python provide us with a short and concise way to construct new sequences (such as lists, set, dictionary etc.) using sequences which have been already defined. Python supports the following 4 types of comprehensions: List Comprehensions. Dictionary Comprehensions.

How do you create a list comprehension in Python?

List Comprehensions provide an elegant way to create new lists. The following is the basic structure of a list comprehension: output_list = [ output_exp for var in input_list if (var satisfies this condition)] Note that list comprehension may or may not contain an if condition.

What is set comprehension?

The set comprehension allows you to create a new set based on an existing set. First, iterate over the elements of a set. Third, create a new set of elements resulting from the expression. In addition, the set comprehension allows you to select which element to apply the expression via a condition in the if clause.

What is the difference between list comprehensions and set comprehensions?

Set comprehensions are pretty similar to list comprehensions. The only difference between them is that set comprehensions use curly brackets { }. Let’s look at the following example to understand set comprehensions. Example #1 : Suppose we want to create an output set which contains only the even numbers that are present in the input list.


2 Answers

primes = {x for x in range(2, 101) if all(x%y for y in range(2, min(x, 11)))} 

I simplified the test a bit - if all(x%y instead of if not any(not x%y

I also limited y's range; there is no point in testing for divisors > sqrt(x). So max(x) == 100 implies max(y) == 10. For x <= 10, y must also be < x.

pairs = {(x, x+2) for x in primes if x+2 in primes} 

Instead of generating pairs of primes and testing them, get one and see if the corresponding higher prime exists.

like image 117
Hugh Bothwell Avatar answered Sep 21 '22 10:09

Hugh Bothwell


You can get clean and clear solutions by building the appropriate predicates as helper functions. In other words, use the Python set-builder notation the same way you would write the answer with regular mathematics set-notation.

The whole idea behind set comprehensions is to let us write and reason in code the same way we do mathematics by hand.

With an appropriate predicate in hand, problem 1 simplifies to:

 low_primes = {x for x in range(1, 100) if is_prime(x)} 

And problem 2 simplifies to:

 low_prime_pairs = {(x, x+2) for x in range(1,100,2) if is_prime(x) and is_prime(x+2)} 

Note how this code is a direct translation of the problem specification, "A Prime Pair is a pair of consecutive odd numbers that are both prime."

P.S. I'm trying to give you the correct problem solving technique without actually giving away the answer to the homework problem.

like image 27
Raymond Hettinger Avatar answered Sep 22 '22 10:09

Raymond Hettinger