Questions tagged [set-comprehension]

A syntactic construct which provides a concise way to create sets in a style similar to the mathematical set-builder notation.

Set comprehensions can be used to construct sets in one line. For simple tasks, a set comprehension may be more readable than sets built using looping constructs.

Set comprehensions tend to consist of an input sequence of either a list, a dictionary or a tuple, variable bindings, filtering predicates and an output expression.

Set comprehensions are very similar to list comprehensions.

57 questions
0
votes
1 answer

Use list/set comprehension merely as a "for" loop?

I am creating a set of NUM_RECORDS tuples in Python. This is my code. record_key_list = {(choice(tuple(studentID_list)), choice(tuple(courseID_list)), randint(2012, 2016), choice(semesters), …
0
votes
2 answers

Need help understanding the behavior of a for loop

I am working through a tutorial on sets in Python 2.7, and I have run into a behavior using a for loop that I do not understand, and I am trying to find out what the reason for the difference in outputs might be. The object of the exercise is to…
AMR
  • 584
  • 1
  • 6
  • 16
0
votes
3 answers

Is there a way to "one-line" this using list/set comprehension?

Here's my classmethod for getting all the subclasses recursively: @classmethod def get_subclasses(cls): subclasses = set() for subclass in cls.__subclasses__(): subclasses.add(subclass) …
Skamah One
  • 2,456
  • 6
  • 21
  • 31
0
votes
1 answer

Is pattern matching in Set Comprehensions in Haskell possible, or what is an alternative?

I am trying to work out if it is possible to pattern match in Haskell set comprehensions. I have a list of lists containing Tuples, or nested lists and tuples; E.G [[(1,("A",1)), (2,("B",1))], [(0,("A",1)), (3,("B",2)), (2,("C",1))]] I want to…
Opentuned
  • 1,477
  • 17
  • 21
0
votes
1 answer

Python generating prime numbers using Set Comprehension

I am working on an assignment in beginning Python calling for a list of primes less then 100 using set comprehension. I can generate non primes using nonPrime = { x for x in range(2, 100) for y in range(2, x) if x % y == 0 } This effectively…
jbolt
  • 688
  • 3
  • 16
  • 37
0
votes
3 answers

Conceptualizing set comprehension

def nfa_eclosure(M, s): """ >>> M = [{'':{1,2,3}}, {'b':{1}}, {'a':{2}}] >>> nfa_eclosure(M, 0) set([0, 1, 2, 3]) """ try: states = {nfa_eclosure(M, x+1) for x in xrange(len(M[s])) if M[s].get('')} except…
Brad Rice
  • 1,334
  • 2
  • 17
  • 36
0
votes
4 answers

How do I add values to a set in a comprehension?

Let's say I have a list of lists of strings (stringList): [['its', 'all', 'ball', 'bearings', 'these', 'days'], ['its', 'all', 'in', 'a', 'days', 'work']] and I also I have a set of strings (stringSet) that are the unique words from…
0
votes
0 answers

Irregularities in Python set comprehensions

Why does this set comprehension... >>> {2**x for x in {0,1,2,3,4,5}} Produce this answer? {32, 1, 2, 4, 8, 16} BUT! This loop... >>> for x in {0,1,2,3,4,5}: ... print 2**x Produces this answer. 1 2 4 8 16 32
Pescolly
  • 922
  • 11
  • 18
0
votes
2 answers

How do I return a regex match that can be iterated repeatedly

I can only seem to iterate over the result val once. Calling length iterates over it, and therefore calling result.next causes an exception. val result = for ( regex(name) <- regex findAllIn output) yield name println(result.length) …
Hamy
  • 20,662
  • 15
  • 74
  • 102
-2
votes
1 answer

Why does my set comprehension throw a TypeError?

I tried to change my original code with this set comprehension next_states = {next_states | self.transition_function[(state, input_value)] for state in e_closure_states} But this code throws TypeError: unhashable type: 'set' Original code…
M.Mar
  • 43
  • 9
-3
votes
3 answers

Can't explain output of print statement in python

I am using set comprehension to calculate prime numbers between 2 to n, n=132 in the code below. As long as the value of variable n is <= 131, the prime numbers generated are printed in proper ascending order, viz., {2,3,5,7,11,...}. Whenever n >…
-4
votes
1 answer

Please enumerate all kinds of comprehensions in Python 3

So far, I've learned about list set dictionary generator comprehensions. Are there any other iterables that can be ''comprehended''? I'm mostly interested in Python 3.
Nibor
  • 1,236
  • 9
  • 23
1 2 3
4