Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an empty set

Tags:

I have some code which tots up a set of selected values. I would like to define an empty set and add to it, but {} keeps turning into a dictionary. I have found if I populate the set with a dummy value I can use it, but it's not very elegant. Can someone tell me the proper way to do this? Thanks.

inversIndex = {'five': {1}, 'ten': {2}, 'twenty': {3},                'two': {0, 1, 2}, 'eight': {2}, 'four': {1},                'six': {1}, 'seven': {1}, 'three': {0, 2},                'nine': {2}, 'twelve': {2}, 'zero': {0, 1, 3},                'eleven': {2}, 'one': {0}}  query = ['four', 'two', 'three']  def orSearch(inverseIndex, query):     b = [ inverseIndex[c] for c in query ]     x = {'dummy'}     for y in b:         { x.add(z) for z in y }     x.remove('dummy')     return x  orSearch(inverseIndex, query) 

{0, 1, 2}

like image 470
Chris Degnen Avatar asked Jul 15 '13 20:07

Chris Degnen


People also ask

How do you create an empty set?

Creating an empty set is a bit tricky. Empty curly braces {} will make an empty dictionary in Python. To make a set without any elements, we use the set() function without any argument.

How do you define an empty set?

An empty set is a finite set since its cardinality is defined and is equal to 0. As we know, a set is said to be infinite if the number of elements in it are infinite, i.e. its cardinality is ∞ or not defined, whereas a finite set contains a countable number of elements.

How do you create an empty list?

You can create an empty list using an empty pair of square brackets [] or the type constructor list() , a built-in function that creates an empty list when no arguments are passed. Square brackets [] are commonly used in Python to create empty lists because it is faster and more concise.

How to create an empty set in JavaScript?

How to Create an Empty Set in JavaScript? To create an empty Set in JavaScript, create a new set using new Set () and pass no arguments to the constructor. The new Set () creates and returns an empty Set. In the following example, we create an empty set using Set () constructor.

How to create a non empty set in Python?

1 print ("Create a new set:") 2 x = set () 3 print (x) 4 print (type (x)) 5 print ("nCreate a non empty set:") 7 more rows ...

What is the nature of Emptyset in Java?

These empty Set are immutable in nature. Following is the declaration of emptySet () method: This method does not accept any parameter. The emptySet () method returns an empty immutable Set.

How to make an empty set in MySQL?

So, to make an empty set, we can use the same method. In set, insertion is accomplished via the set.add () function, which generates an appropriate record value for storage in the hash table. The same as searching for a specific item, i.e., O (1) on average. However, in the worst-case scenario, it can become O (n).


2 Answers

You can just construct a set:

>>> s = set() 

will do the job.

like image 77
Jon Kiparsky Avatar answered Oct 03 '22 17:10

Jon Kiparsky


The "proper" way to do it:

myset = set() 

The {...} notation cannot be used to initialize an empty set

like image 32
inspectorG4dget Avatar answered Oct 03 '22 18:10

inspectorG4dget