Sets and strings

Sets and strings

aggregate

1. Recognition set

'''

A collection is a container type data type. Take {} as the flag of the container. Multiple data in it are separated by commas: {data 1, data 2, data 3,...}

Variable (support addition, deletion and modification); Unordered (subscript operation is not supported)

Requirements for elements: 1 Must be immutable data 2 Element is unique

'''

#Empty set

set1 = set()

print(type(set1),lent(set1))

#The collection is out of order

print({1,2,3} == {3,2,1}) #True

#Collection elements must be data of immutable type

set1 = {1,'abc',True,[10,20]}

print(set1)
#set2 = {1,'abc',True,[10,20]}     #report errors

#Set is unique

set = {10,20,10,20,30,10}
print(set3)    #{10,20,30}

names = ['Zhang San','Li Si','Zhang San','Wang Wu','Zhang San']
print(set(names))

2. Addition, deletion, modification and query of collection

1) Search - can only traverse

#Supplement: when traversing the unordered sequence with the for loop, the system will automatically convert the unordered sequence into a list before traversing, and then traverse the list

set1  = {'Attacking giants','About three years','Under one person','One Piece'}
for x in set1:
	print(x)
	
dict1 = {'a' : 10, 'b' : 20, 'c' : 30}
print(list(dict1))
for i in dict1:
	print(i)

2) Increase

'''

Assemble Add (element) - adds the specified element

Assemble Update - adds all the elements in the sequence to the collection

'''

set1.add('death')
print(set1)

set1.update('abc')
print(set1)

3) Delete

'''

Assemble Remove (element)

'''

set1  = {'Attacking giants','About three years','Under one person','One Piece'}

set1.remove('One Piece')

print(set1)

3. Mathematical set operation

Sets and mathematical sets used in python are one thing, and support mathematical set operations

Mathematical set operations supported by python: intersection (&), Union (|), difference (-)

Symmetric difference set (^), subset (> =, < =), true subset (>, <)

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG oousvgxf-1646395257849) (E: \ qfstudy \ image \ set operation. png)]

[the external chain image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-i5OyC16E-1646395257851)(E:\QFstudy\image \ set operation 1.png)]

set1 = {1,2,3,4,5}
set1 = {3,4,5,6,7,8}

1) Intersection - gets the common part of two sets

print(set1 & set2)            #{3,4,5}

2) Union - combines two sets into a whole

print(set1 | set2)              #{1,2,3,4,5,6,7,8}

3) Difference set - get the rest of set 1 after removing the part contained in set 2

print(set1 - set2)               #{1,2}

print(set2 - set1)			   #{6,7,8}

4) Symmetric difference sets - merge two sets and remove the common part

print(set1 ^ set2)               #{1,2,6,7,8}

5) Subsets and proper subsets

#Set 1 > Set 2 - determines whether set 2 is a true subset of set 1

#Set 2 > Set 1 - determines whether set 1 is a true subset of set 2

print({1,2} < {1,2,2})          #True
print({100,200,300,400,1,2} > {1,2})          #True

#Positive subset of {1,2,3}: {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}

#{1,2,3} subset: {}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, 1,2,3}

#Exercise: use three sets to represent the names of students who choose courses in three disciplines (a student can choose multiple courses at the same time)

python = {'Xiao Ming','floret','Xiao Hong','Xiaoyi'}
java = {'Xiao Ming','Xiao Hong'}
c = {'Xiao Ming','floret'}

# 1. How many students are there in total

set1 = python | java | c
print(len(set1))

# 2. Find the number of people who have selected only the first subject and their corresponding names

print("Number of people in the first discipline:",len(python))
for x in python:
    print(x, end=' ')

# 3. Find the number of students who have chosen only one subject and their corresponding names

set2 = python & java & c
set3 = python ^ java ^ c
print("Number of students who chose only one subject:", len(set3 - set2))
print("What is the name of a student who has chosen only one subject:", (set3 - set2))

# 4. Find the number of students who have chosen only two subjects and their corresponding names

set5 = set1 - set4 - set2
print("Number of students who chose three subjects:",len(set5),"Corresponding name:",set5)

# 5. Find the number of students who have selected three courses and their corresponding names

print("\n Number of students who chose three subjects:",len(set2),"\n Students in three subjects:",set2)

character string

Recognize string, character and string query

1. What is a string (str)

'''

String is a container type data type. Take '', '', '', '', and '' as the marks of the container. Each symbol on the quotation mark is the element of the string (the element character of the string)

Immutable (addition, deletion and modification are not supported), orderly (subscript operation is supported)

Elements of string: all symbols can be used as elements of string, and the elements of string are also called characters. There are two types of characters: ordinary characters and escape characters

'''

#Empty string

str = ''        #0

str = ' '       #1


#The character can be any symbol

2. Character - each independent symbol in the string is a character

"""

Characters are divided into ordinary characters and escape characters

1) Ordinary characters - characters representing the symbol itself are ordinary characters

2) Escape character - a symbol with a special function or meaning is an escape character. General format of all escape characters: \ X, \ XXXX (escape characters are formed by adding \ to ordinary characters, but not all ordinary characters can be changed into escape characters by adding \ before them)

\n - line feed

\t - Horizontal tab (equivalent to pressing tab once)

"- a double quotation mark

’-A single quotation mark

\- a backslash

Note: the length of all escape characters is 1

"""

#\u4 digit hexadecimal number - encoded character (character encoding corresponding to 4-digit hexadecimal)

#r grammar

If you add r/R to the front of a string, you can make all the escape characters in the character disappear, and each character becomes an ordinary character

#Hexadecimal number - the number on each bit can be 0-9 or a-f(A-F)

ptint('a')
print('\n')

print('i say:\'you see see,one day day!\'')

#Cancel escape character
print('\\abc\\n123')
print(r'\abc\n123')

print('\u00FF')

3. Character coding

#a -> 97

1). Principle of computer data storage

"""

The data that the computer can directly store is only numbers. When saving numbers, it saves the complement of numbers

If you want to save non digital data to the computer, you must first convert the data into numbers when storing.

If you need to store text, the computer will save the corresponding coding value of the text.

"""

2). Character encoding

"""

Each text symbol (character) corresponds to a fixed number in the computer, and this number is the coded value of this symbol.

"""

3). Character coding table - a table that records the correspondence between each symbol and number

"""

Common code table: ASCII code table, Unicode code table (Python)

#ASCII code table

ASCII code table only has 128 characters (not enough)

In ASCII code table: numeric characters are in front of letters, uppercase letters are in front of primary school letters, there is a gap between uppercase letters and lowercase characters, numbers increase continuously from 0, and letters increase from (a/A)

a-97

A-65

#Unicode code table - is an extension of ASCII code table, which contains ASCII code.

The Unicode code table contains all symbols (Universal codes) of all countries and nationalities in the world

Chinese code range: 4e00 ~ 9fa5

"""

Application of coded value in python

"""

#chr function: chr (code value) - obtain the corresponding character according to the code value; Coded value - integer

#Ord function: ord (character) - get the encoding value corresponding to the character; Character - a string with a length of 1

#Coded character

"""

#The program directly represents a hexadecimal number: prefix 0x/0x

#Hex - converts a number to hexadecimal

print(ord('a'))
print(chr(97),chr(65))
print(chr(0x2800))
for i in range(0x2800,0x28ff+1):
	print(chr(i))

#Coded character

str1 = 'a\u0061 one\u4e00'
print(str1)

#Judge whether the specified character is Chinese

ch = 'a'
print('\u4e00' <= ch <= '\u9fa5')
print(chr(0x9fa5))

4. Common string operations

#1. Query - get characters

The string is as like as two peas for getting characters and lists to get elements.

str1 = 'hello world!'
print(str[1],str1[-1])


str2 = '\t123\u5fa2abc'
print(str2[5])


#2. String related operations

#1)+,*

#2) Compare size

"""

The two strings compare the size, which is the size of the encoded value of the first pair of unequal characters;

Application: judge the nature of characters

a. Judge whether the character x is a numeric character: '0' < = x < = '9'

b. Judge whether the character x is lowercase: 'a' < = x < = 'z'

c. Judge whether the character x is A capital letter: 'A' < = x < = 'Z'

d. Judge whether the character x is a letter: 'a' < = x < = 'Z' or 'a' < = x < = 'Z'

e. Judge whether the character x is Chinese: '\ u4e00' < = x < = '\ u9fa5'

"""

print('abc' > 'mn')

#3) In and not in

#String 1 in string 2 - determines whether string 1 is in string 2 (determines whether string 1 is a string of string 2)

str1 = 'abc123'        #True
print('a' in str1)     #True
pritn('abc' in str1)   #True
print('13' in str1)    #False

The character string compares the size, which is the size of the encoded value of the first pair of unequal characters;

Application: judge the nature of characters

a. Judge whether the character x is a numeric character: '0' < = x < = '9'

b. Judge whether the character x is lowercase: 'a' < = x < = 'z'

c. Judge whether the character x is A capital letter: 'A' < = x < = 'Z'

d. Judge whether the character x is a letter: 'a' < = x < = 'Z' or 'a' < = x < = 'Z'

e. Judge whether the character x is Chinese: '\ u4e00' < = x < = '\ u9fa5'

"""

print('abc' > 'mn')

#3) In and not in

#String 1 in string 2 - determines whether string 1 is in string 2 (determines whether string 1 is a string of string 2)

str1 = 'abc123'        #True
print('a' in str1)     #True
pritn('abc' in str1)   #True
print('13' in str1)    #False

Keywords: Python Algorithm leetcode

Added by olko on Sat, 05 Mar 2022 00:59:17 +0200