Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python sets

# You can't create a set like this in Python
my_set = {} # ---- This is a Dictionary/Hashmap

# To create a empty set you have to use the built in method:
my_set = set() # Correct!


set_example = {1,3,2,5,3,6}
print(set_example)

# OUTPUT
# {1,3,2,5,6} ---- Sets do not contain duplicates and are unordered
Comment

python sets


empty_set = set()

basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}

# sets do not contain duplicates
print(basket)                      
# output {'orange', 'banana', 'pear', 'apple'}

# check if orange is in basket set
print('orange' in basket)
# output true

# convert a string to a set of letters - sets contains no duplicates
set_a = set('abcd')
set_b = set('bcde')

# the operations below returns new sets
# print letters in set_a but not in set_b - difference
print(set_a - set_b)
# output {'a'}

# print set letters that is in either set a or b - union
print(set_a | set_b)
# output {'a', 'c', 'e', 'b', 'd'}

# print letters that are in both set_a and set_b - intersection
print(set_a & set_b)
# output {'c', 'd', 'b'}

# print letters that are in set_a and set_b when the letters are found in a set but no the other set - symmetric_difference()
print(set_a ^ set_b)
# output {'a', 'e'}
Comment

sets in python

set_of_base10_numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
set_of_base2_numbers = {1, 0}

intersection = set_of_base10_numbers.intersection(set_of_base2_numbers)
union = set_of_base10_numbers.union(set_of_base2_numbers)

'''
intersection: {0, 1}:
	if the number is contained in both sets it becomes part of the intersection
union: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}:
	if the number exists in at lease one of the sets it becomes part of the union
'''
Comment

when to use python sets

"""
The Python sets are highly useful to efficiently remove duplicate values from
a collection like a list and to perform common math operations like unions and
intersections.
"""
Comment

python sets

# Python also has sets which you can use
# Keep in mind that sets cannot have duplicate of the same value
# This can be useful for removing doubles in a list

mySet = {1, 2, 2, 3, 4, 5, 10, 10, 15}
print(mySet) # {1, 2, 3, 4, 5, 10, 15}

# You can also create a set with 'set([list elements])'

myList = ['apples', 'bananas', 'oranges', 'grapes']
myList.append('apples')
myList.append('oranges')
print(myList) # ['apples', 'bananas', 'oranges', 'grapes', 'apples', 'oranges']

myNewSet = set(myList)
print(myNewSet) # {'apples', 'bananas', 'oranges', 'grapes'}
Comment

How to Create Sets in Python

nameSet = {"John", "Jane", "Doe"}

print(nameSet)
# {'Jane', 'Doe', 'John'}
Comment

Python Sets

# Python program to demonstrate
# Creation of Set in Python
 
# Creating a Set
set1 = set()
print("Initial blank Set: ")
print(set1)
 
# Creating a Set with
# the use of a String
set1 = set("GeeksForGeeks")
print("
Set with the use of String: ")
print(set1)
 
# Creating a Set with
# the use of Constructor
# (Using object to Store String)
String = 'GeeksForGeeks'
set1 = set(String)
print("
Set with the use of an Object: " )
print(set1)
 
# Creating a Set with
# the use of a List
set1 = set(["Geeks", "For", "Geeks"])
print("
Set with the use of List: ")
print(set1)
Comment

Python Sets

# Python program to demonstrate
# Creation of Array
 
# importing "array" for array creations
import array as arr
 
# creating an array with integer type
a = arr.array('i', [1, 2, 3])
 
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
    print (a[i], end =" ")
print()
 
# creating an array with float type
b = arr.array('d', [2.5, 3.2, 3.3])
 
# printing original array
print ("The new created array is : ", end =" ")
for i in range (0, 3):
    print (b[i], end =" ")
Comment

PREVIOUS NEXT
Code Example
Python :: select columns rsnge dataframe 
Python :: pandas form multiindex to column 
Python :: Python Program to Find sum Factorial of Number Using Recursion 
Python :: how to increment datetime by custom months in python 
Python :: python macro ensurepip py3 
Python :: how to test webhook in python.py 
Python :: summation 
Python :: best api for python 
Python :: image segmentation pyimagesearch 
Python :: Remove all duplicates words from a given sentence 
Python :: python pytest use same tests for multiple modules 
Python :: folium add a polygon to a map 
Python :: save csv with today date pandas 
Python :: lib.stride_tricks.sliding_window_view(x, window_shape, axis=None, *, subok=False, writeable=False) 
Python :: Iterate through string with index in python using while loop and rang 
Python :: sklearn mahalanobis distance 
Python :: rtdpy ncstr 
Python :: calendar range 
Python :: should i learn c++ or python 
Python :: yticks in plotly expres 
Python :: connect two mathod to the same button in pyq5 
Python :: python: if null give a value if not null concatenate 
Python :: pdfkit supress output 
Python :: medium how to interact with jupyter 
Python :: python hash md5 unicode 
Python :: inspect last 5 rows of dataframe 
Python :: fibonacci sequence generator python 
Python :: indentation error in python atom editor 
Python :: django is .get lazy 
Python :: how fast is iglob 
ADD CONTENT
Topic
Content
Source link
Name
9+3 =