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
2
votes
1 answer

Which microversion of Python 2.7 introduced set comprehension?

It works in my interpreter (2.7.12). From http://sopython.com/wiki/In_which_Python_version_was_feature_X_introduced%3F I know, that it was introduced in 2.7. Unfortunately the online documentation is available only for 2.7.13, so I can not check in…
abukaj
  • 2,582
  • 1
  • 22
  • 45
2
votes
2 answers

Set comprehension gives "unhashable type" (set of list) in Python

I have the following list of tuples: list_of_tuples = [('True', 100, 'Text1'), ('False', 101, 'Text2'), ('True', 102, 'Text3')] I want to collect all second elements of each tuple into a set: my_set =…
Hendrik
  • 1,158
  • 4
  • 15
  • 30
2
votes
2 answers

Set comprehensions in Python and testing for membership in the set being created

This is actually question about the semantics of set comprehensions, but I first need to explain the context. I'm trying to create a new set of tuples where the paired value in the touple is unique regardless of the order of the values in the pair.…
Chris Brendel
  • 700
  • 3
  • 10
1
vote
2 answers

Python comprehension with multiple prints

I want to put this for loop into a comprehension. Is this even possible? for i in range(1, 11): result = len({tuple(walk) for (walk, distance) in dic[i] if distance == 0}) print(f'There are {result} different unique walks with length…
1
vote
1 answer

Trying to populate a dictionary within a dictionary

I am reading a string from a file in my system. The string is as follows: A -> B [ label="5.000" ]; B -> A [ label="-5.000" ]; I want to populate a set node and dictionary adj. Here's my code: node = set() adj = {} with open("tiny_stn.dot",'r+t')…
1
vote
4 answers

How to compare a string to any object in the list?

Name_list = [ ['Name 1', 'Name 2', 'Name 3'] ] Name = 'Name 3' if Name == 'any name in the list': print('Name is in the list') How can I check if "Name 3" is in the list? And also when the list looks like this: list_1 = [ ['Name…
1
vote
1 answer

set comprehension vs. nested loop

I have a list of strings, each having one or more words. I need to make a list of unique words out of this list. I can do it easily with two nested loops but I don't understand why I don't get the same result using a set comprehension. Nested…
miro
  • 41
  • 4
1
vote
1 answer

How to define powerset in Data.Set.Monad?

When using Data.Set.Monad together with {-# LANGUAGE MonadComprehensions} one can define sets almost like we did in high school, where we defined sets using comprehensions like {x ∈ S | φ(x)}. For example: s' = [x | x <- s, phi(x)] This is not…
Hans Lub
  • 5,513
  • 1
  • 23
  • 43
1
vote
2 answers

Cache variable in list comprehension

Lets say I have an expensive operation expensive(x: int) -> int and the following list comprehension: # expensive(x: int) -> int # check(x: int) -> bool [expensive(i) for i in range(LARGE_NUMBER) if check(expensive(i))] If I want to avoid running…
1
vote
1 answer

Function with generic comprehension return type depending on input argument?

Assuming I have a function that converts a character in a collection of strings like - from typing import Collection def replace_in_collection(input_collection: Collection[str]) -> Collection[str]: return [x.replace('a', 'b') for x in…
1
vote
1 answer

Python 3.5 For loop Set return method

I do not understand what these lines are doing. S = {-4 , 4 ,-3 , 3, -2 , 2, -1, 1, 0}; {x for x in S if x >= 0} I know S is a set. I know we are looping through set S but what I don't understand is what is the "x" doing before the for loop? And…
1
vote
2 answers

(Set) Comprehension from multiple values

Suppose I’ve got a list l = [1,2,3] and I want to create a set of all numbers in that list and their squares. Ideally, in a single comprehension expression. Best I can come up with is (two iterations over the list): set(_ for _ in l).union(_ * _ for…
Jens
  • 8,423
  • 9
  • 58
  • 78
1
vote
1 answer

Set comprehension and different comparable relations

I have set objects which are comparable in some way and I want to remove objects from the set. I thought about how this problem changes, for different comparable relations between the elements. I was interested in the development of the search…
Mac C.
  • 153
  • 10
0
votes
2 answers

Python: How to remove/discard a string from a set of strings using case-insensitive match?

I have a case from Wikidata where the string Articles containing video clips shows up in a set of categories and needs to be removed. Trouble is, it also shows up in other sets as articles containing video clips (lowercase "a"). The simple/safe way…
mfeb
  • 3
  • 3
0
votes
1 answer

Return dataframes containing unique column pairs in Pandas?

I am trying to use pandas to select rows based on unique column pairs. For example with the dataframe below read of of an csv: col1 col2 col3 0 1 10 [a, b, c, d] 1 1 10 [e, f, g, h] 2 2 11 [a, b, c, d] 3 3…
Bob R
  • 605
  • 1
  • 13
  • 25