Python Learning Notes - Collection

aggregate


Notes are a link to the original tutorial that I made while studying for the convenience of reviewing later. Python 3 Tutorial | Rookie Bird Tutorial
A set is an unordered sequence of nonrepeating elements
Create a collection using curly brackets {} or set() functions, note that creating an empty collection must use set() instead of {} because {} is used to create an empty dictionary
Create format:
parame = {value1,value2,...}
perhaps
set(value)

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)                #The set is unordered and not duplicated, and the order of each output is random
print('orange' in fruit)    #Determine whether an element is in a collection
#Operations between sets
a = set('123456')
b = set('1357984')
print(a-b)                  #Elements contained in set a but not in set b
print(a|b)                  #All elements contained in collection a or b
print(a&b)                  #Elements contained in collections a and b
print(a^b)                  #Elements that are not contained in both a and b

Run Results
{'pear', 'apple', 'orange', 'banana'}
True
{'6', '2'}
{'1', '8', '4', '6', '3', '9', '7', '2', '5'}
{'1', '3', '5', '4'}
{'6', '9', '7', '2', '8'}

Basic operations for collections

Add Elements

s.add(x) add s element X to the set s and does nothing if the element already exists

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)
fruit.add('strawberry')
print(fruit)
fruit.add('apple')
print(fruit)

Run Results
{'orange', 'pear', 'apple', 'banana'}
{'banana', 'orange', 'apple', 'strawberry', 'pear'}
{'banana', 'orange', 'apple', 'strawberry', 'pear'}

s.update(x1,x2,...) #Parameters can be lists, tuples, dictionaries, etc.

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)
fruit.update(('strawberry',),(1,2,'hello'),[4,5,6])
print(fruit)

Run Results
{'orange', 'apple', 'banana', 'pear'}
{1, 'pear', 'strawberry', 'banana', 2, 4, 5, 6, 'apple', 'orange', 'hello'}

Removing Elements

s.remove(x) #Removes element x from set s, and errors occur if the element does not exist

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)
fruit.remove('apple')
print(fruit)
fruit.remove('hello')
print(fruit)

Run Results
{'apple', 'banana', 'orange', 'pear'}
{'banana', 'orange', 'pear'}
Traceback (most recent call last):
File "app.py", line 5, in <module>
fruit.remove('hello')
KeyError: 'hello'

s.discard(x) #If the element does not exist, no error will occur

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)
fruit.remove('apple')
print(fruit)
fruit.remove('hello')
print(fruit)

Run Results
{'banana', 'pear', 'orange', 'apple'}
{'banana', 'pear', 'orange'}
{'banana', 'pear', 'orange'}

s.pop() #Randomly delete an element in a set

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)
x = fruit.pop()
print(x)
print(fruit)

Run Results
{'apple', 'pear', 'orange', 'banana'}
apple
{'pear', 'orange', 'banana'}

Calculates the number of collection elements

len(s)

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)
print(len(fruit))

Run Results
{'orange', 'apple', 'banana', 'pear'}
4

Empty Collection

s.clear()

fruit = {'apple','banana','orange','pear','banana'}
print(fruit)
fruit.clear()
print(fruit)

Run Results
{'orange', 'banana', 'apple', 'pear'}
set()

Determine if an element exists in a collection

x in s

fruit = {'apple','banana','orange','pear','banana'}
print('apple' in fruit)
print('hello' in fruit)

Run Results
True
False

9 original articles published. 0% praised. 97% visited
Private letter follow

Tags: Python

Posted on Sat, 08 Feb 2020 21:59:27 -0500 by alpine