程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python set collection

編輯:Python

List of articles

  • Python set aggregate
    • One ,Python establish set aggregate
      • 1) Use {} establish
      • 2) set() Function to create a collection
    • Two ,Python visit set Collection elements
    • 3、 ... and ,Python Delete set aggregate


Python set aggregate

Python The set in , Like the set concept in Mathematics , Used to hold elements that don't repeat , That is, the elements in the collection are unique , Different from each other .

In form , It's similar to a dictionary ,Python The collection places all elements in a pair of braces {} in , Use... Between adjacent elements “,” Separate , As shown below :

{
element1,element2,...,elementn}

among ,elementn Represents an element in a collection , There is no limit to the number .

In terms of content , In the same set , Only immutable data types can be stored , Including plastic surgery 、 floating-point 、 character string 、 Tuples , Can't store list 、 Dictionaries 、 Set these variable data types , otherwise Python The interpreter will throw TypeError error . for instance :

>>> {
{
'a':1}}
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
{
{
'a':1}}
TypeError: unhashable type: 'dict'
>>> {
[1,2,3]}
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
{
[1,2,3]}
TypeError: unhashable type: 'list'
>>> {
{
1,2,3}}
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
{
{
1,2,3}}
TypeError: unhashable type: 'set'

And it's important to note that , Data must be guaranteed to be unique , Because collections are for every data element , Only one copy will be kept . for example :

>>> {
1,2,1,(1,2,3),'c','c'}
{
1, 2, 'c', (1, 2, 3)}

because Python Medium set Sets are unordered , So the order of elements may be different each time they are output .

Actually ,Python There are two types of sets in , One is set A collection of types , The other is frozenset A collection of types , The only difference between them is ,set Type sets can be added 、 Delete element operation , and forzenset Type set is not good . This section begins with set Type set , In the following chapters forzenset Type set .

One ,Python establish set aggregate

Python Provides 2 Species creation set The way to assemble , They are using {} Create and use set() The function will list 、 Tuples and other types of data are converted to sets .

1) Use {} establish

stay Python in , establish set Collections can be like lists 、 Elements are the same as dictionaries , Assign the set directly to the variable , To achieve the purpose of creating a collection , The syntax is as follows :

setname = {element1,element2,…,elementn}
among ,setname Represents the name of the collection , When you name it, you have to match Python Naming specification , Also avoid the relationship with Python Duplicate name of built-in function .

for instance :

a = {
1,'c',1,(1,2,3),'c'}
print(a)

The running result is :

{1, ‘c’, (1, 2, 3)}

2) set() Function to create a collection

set() Function is Python Built in functions for , Its function is to string 、 list 、 Tuples 、range Objects and so on can iterate objects into sets . The syntax format of this function is as follows :

setname = set(iteration)

among ,iteration It means string 、 list 、 Tuples 、range Object and other data .

for example :

set1 = set("c.biancheng.net")
set2 = set([1,2,3,4,5])
set3 = set((1,2,3,4,5))
print("set1:",set1)
print("set2:",set2)
print("set3:",set3)

The running result is :

set1: {‘a’, ‘g’, ‘b’, ‘c’, ‘n’, ‘h’, ‘.’, ‘t’, ‘i’, ‘e’}
set2: {1, 2, 3, 4, 5}
set3: {1, 2, 3, 4, 5}

Be careful , If you want to create an empty set , Only use set() Function implementation . Because use a pair of {},Python The interpreter will treat it as an empty dictionary .

Two ,Python visit set Collection elements

Because the elements in the set are unordered , So you can't use subscripts to access elements like lists .Python in , The most common way to access collection elements is to use a circular structure , Read out the data in the collection one by one .

for example :

a = {
1,'c',1,(1,2,3),'c'}
for ele in a:
print(ele,end=' ')

The running result is :

1 c (1, 2, 3)

Because we haven't learned the cycle structure yet , The above code beginners only need to have a preliminary understanding of , After learning the structure of the following cycle, you will naturally understand .

3、 ... and ,Python Delete set aggregate

Like other sequence types , Manual function collection type , You can also use del() sentence , for example :

a = {
1,'c',1,(1,2,3),'c'}
print(a)
del(a)
print(a)

The running result is :

{1, ‘c’, (1, 2, 3)}
Traceback (most recent call last):
File “C:\Users\mengma\Desktop\1.py”, line 4, in
print(a)
NameError: name ‘a’ is not defined

Python set The most common operation of a collection is to add 、 Remove elements , And the intersection between sets 、 Combine 、 Subtraction and so on . Limited by space , This knowledge will be explained in the next section .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved