#1、 Create set
Example 1: establish
s = {
1,2,3,1,2,3}
print(s,type(s))
# result
{
1, 2, 3} <class 'set'>
Example 2: There can be no other variables in the set
s = {
1,2,3,[1,2,3]}
print(s,type(s))
# result
TypeError: unhashable type: 'list'
Be careful : For empty sets set
s={
}
print(s,type(s)) The result is a dictionary
k=set()
print(k,type(k)) set The result of is the set
# result
{
} <class 'dict'>
set() <class 'set'>
Because the set is out of order , Not repeated , therefore
Connection is not supported , repeat , Indexes , section
Support member operators (in ,not in)
1、add Add one more
s = {
1,2,3}
s.add(100)
print(s)
# result
{
1, 2, 3, 100}
2、update Add more than one at a time
s = {
1,2,3}
s.update({
4,5,6})
print(s)
# result
{
1, 2, 3, 4, 5, 6}
1、remove: Delete ( If there is no deleted number, an error will be reported )
s = {
1,2,3}
s.remove(3)
print(s)
# result
{
1, 2}
2、discard: Delete ( If there is no number, do not operate )
s = {
1,2,3}
s.discard(3)
print(s)
# result
{
1, 2}
3、pop: Delete any value ( If the collection is empty, delete )
s = {
1,2,3,4,5}
s.pop()
print(s)
# result
{
2, 3, 4, 5}
1、 Difference set
s1={
1,2,3,4}
s2={
3,4}
print(s1-s2)
# result
{
1, 2}
2、 intersection
s1={
1,2,3,4}
s2={
3,4}
print(s1-s2)
print(s1 & s2)
# result
{
3, 4}
3、 Symmetric difference
s1={
1,2,3}
s2={
1,2,4}
print(s1^s2)
# result
{
3, 4}
4、 Combine
s1={
1,2,3}
s2={
1,2,4}
print(s1|s2)
# result
{
1, 2, 3, 4}
5、 Judging subsets
s1={
1,2,3}
s2={
1,2,4}
print(s1.issubset(s2)) s1 yes s2 A subset of
# result
False
6、 Judge the intersection
s1={
1,2,3}
s2={
1,2,4}
print(s1.isdisjoint(s2)) s1 And s2 There is no intersection, right
# result
False
7、 expand :frozenset
Don't add , Delete
s = frozenset({1,2,3})
print((s,type(s)))
# result
(frozenset({1, 2, 3}), <class ‘frozenset’>)
import random
NUMBER=set()
print(NUMBER,type(NUMBER))
for x in range(100):
num = random.randint(1,1000)
NUMBER.add(num)
print(NUMBER)
print(sorted(NUMBER,reverse=True))
1、 Dictionary creation
key-value Right or key value right
d = {
"name":"westos","age":18,"city":" Xi'an "}
print(d,type(d))
# result
{
'name': 'westos', 'age': 18, 'city': ' Xi'an '} <class 'dict'>
2、 Create an empty dictionary
d={
}
print(d,type(d))
# result
{
} <class 'dict'>
Similar to a collection , Splicing is not supported , Repetition, etc
1、 Judge member operators
d = {
"name":"westos","age":18,"city":" Xi'an "}
print('name' in d)
print('westos' in d)
# result
True Is a judgment key It's worth it , So the answer is ture
False yes westos No key It's worth it , So the answer is false
1、 see
d = {
"name":"westos","age":18,"city":" Xi'an "}
print(d.keys()) View all that can 、 value
print(d.values()) View all value
print(d.items()) View all elements
print(d['name']) see name Corresponding value value
print(d.get('province')) View provinces 、 Corresponding return value value , Return if present , If it does not exist, it will be returned as None
print(d.get('province',' shaanxi ')) View identity , If not, return the default value
# result
dict_keys(['name', 'age', 'city'])
dict_values(['westos', 18, ' Xi'an '])
dict_items([('name', 'westos'), ('age', 18), ('city', ' Xi'an ')])
westos
shaanxi
2、 Add and modify
d = {
"name":"westos","age":18,"city":" Xi'an "}
print(d)
d['privince']=' shaanxi ' If not, add
print(d)
d['privince']=' Shandong ' Modify if any
print(d)
# result
{
'name': 'westos', 'age': 18, 'city': ' Xi'an '}
{
'name': 'westos', 'age': 18, 'city': ' Xi'an ', 'privince': ' shaanxi '}
{
'name': 'westos', 'age': 18, 'city': ' Xi'an ', 'privince': ' Shandong '}
{
'name': 'westos', 'age': 18}
d.setdefault('city',' Xi'an ') Add if not
print(d)
d.setdefault('city',' Beijing ') If there is key value , Do not operate
print(d)
# result
{
'name': 'westos', 'age': 18, 'city': ' Xi'an '}
{
'name': 'westos', 'age': 18, 'city': ' Xi'an '}
3、 Delete
d = {
'name': 'westos', 'age': 18}
del d['name']
print(d)
# result
{
'age': 18}
d = {
'name': 'westos', 'age': 18}
d.pop('name')
print(d)
# result
{
'age': 18}
4、 Traverse
General traversal will only traverse out key value
d = {
"name":"westos","age":18,"city":" Xi'an "}
for items in d:
print(items)
# result
name
age
city
Traverse all
d = {
"name":"westos","age":18,"city":" Xi'an "}
for item in d.items():
print(item)
# result
('name', 'westos')
('age', 18)
('city', ' Xi'an ')
Traverse out key and value( Very important )
d = {
"name":"westos","age":18,"city":" Xi'an "}
for key,value in d.items():
print(f'key={
key},value={
value}')
# result
key=name,value=westos
key=age,value=18
key=city,value= Xi'an
5、 Default dictionary
(1)、int integer
Set default value value , Defined here int integer
from collections import defaultdict
d = defaultdict(int)
print(d)
print(d['views'])
# result
defaultdict(<class 'int'>, {
})
0
In this way, it can be supplemented directly
from collections import defaultdict
d = defaultdict(int)
d['view'] +=1
d['transfer'] +=1
print(d)
# result
defaultdict(<class 'int'>, {
'view': 1, 'transfer': 1})
(2)、list type
This is set as the list type , You can use the list append Add
d = defaultdict(list)
d['allow_users'].append('westos')
d['deny_users'].extend('ck1,ck2')
print(d)
# result
defaultdict(<class 'list'>, {
'allow_users': ['westos'], 'deny_users': ['ck1', 'ck2']})
(3)、set Collection types
defaultdict(<class ‘set’>, {‘love_movies’: {‘ The former 3’, ‘ The movie xxx’, ‘ The matrix ’}})
The properties of ordered sequences : Indexes , section , Link operator , Some features of the repetition operator and the member operator .
1、 Variable and immutable (a assignment b,a After changing the value ,b Whether the address can be changed )
Variable data type :list,set,dict
Immutable data types : The number , Tuples
2、 Sequence
see ppt