-20 python learning collection

                           Collection set

1. The collection of different elements, unordered collection is a group of arrayed elements of the collection must be immutable

- the definition of a collection

The first:

i = {1,2,3,4 }
 print (type (i), i)

operation result:

<class 'set'> {1, 2, 3, 4}

Process finished with exit code 0

The second:

jh = set('hello')
print(type(jh),jh)

operation result:

<class 'set'> {'e', 'o', 'h', 'l'}

Process finished with exit code 0

 

- Add elements

jh = {1,2,3,4}
jh.add("nihao")
print(jh)
{1, 2, 3, 4, 'nihao'}

Process finished with exit code 0

 

 

-delete

* .Clear Clear collection

 

 

* .Pop randomly deleted

i = {1,2,3,4 } 
Jkpop () 
print (i)

operation result:

{2, 3, 4}

Process finished with exit code 0

 

* .Remove delete the specified element (if the element is not specified, it will error)

jh = {1,2,3,4 } 
jh.remove ( 4 )
 print (jh)

operation result:

{1, 2, 3}

Process finished with exit code 0

 

* .Discard delete the specified element (if the element is not specified, no error)

 

 

- Intersection &

math = {'xm','xh','xg'}
english ={'xm','xh'}

print(math.intersection(english))

operation result:

{'xh', 'xm'}

Process finished with exit code 0

 

- union |

math = {'xm','xh','xg','xx'}
english ={'xm','xh','dm','john'}

print(math.union(english))

operation result:

{'xg', 'dm', 'john', 'xm', 'xx', 'xh'}

Process finished with exit code 0

 

- difference set (two sets may be subtracted)

math = {'xm','xh','xg','xx'}
english ={'xm','xh','dm','john'}

print(math.difference(english))
print(english.difference(math))

operation result:

{'xg', 'xx'}
{'dm', 'john'}

Process finished with exit code 0

 

Guess you like

Origin www.cnblogs.com/liujinjing521/p/11112793.html