day9 sets and strings

Posted by fimbria on Thu, 24 Feb 2022 14:02:55 +0100

day9 sets and strings

1, Assemble

1. Recognition set

Collection is a container data type, with {} as the flag of the container

Multiple data are separated by commas: {data 1, data 2,...}

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

Requirements for elements:

1) Must be immutable data

2) Element is unique

2. Empty set - set cannot be represented by {}

set2 = set() #Represents an empty set

3. The set is out of order

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

4. The elements of the set must be data of immutable type

set1 = {1, 'abc', True, (10, 20)} 
set1 = {1,'abc',True,[10,20]} #TypeError: unhashable type: 'list'

5. Set elements are unique (the combination of two sets will automatically remove duplication)

set3 = {10, 10, 20, 30, 40, 40, 50}
print(set3)  # {'abc', 1, (10, 20)} is automatically de duplicated because the elements of the set are unique

6. Addition, deletion, modification and query of collection

1) Search - can only traverse

2) Add - set Add (element) set update (element)

3) Delete - set remove (element)

7. Mathematical set operation

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

# Intersection (&) - or take the common elements in two sets
print(set5 & set2)
# Union (|) - combines two sets into a single set
print(set5 | set2)
# Difference set (-) - get the rest of set 1 after removing the part contained in set 2
print(set5 - set2)
# Symmetry difference (^) - merge two sets and remove the common part
print(set5 ^ set2)
 Subset(>= , <=),True subset(> , <)
# Set 1 > Set 2 - determines whether set 2 is a true subset of set 1
# Set 1 < set 2 - determines whether set 1 is a true subset of set 2
print({1, 2, 3, 4} > {1, 2, 3})

2, String

1. What is a string (str)

String is a container data type, with ',' ',' ',' 'as container flags

Every symbol in quotation marks is an element of a string (the element of a string is called a character)

Immutable (adding, deleting and modifying operations are not supported), orderly (subscript operations are supported)

String elements: all symbols can be used as string elements. String elements are also called characters. Characters are divided into two types: ordinary characters and escape characters

'''Characters are divided into ordinary characters and escape characters
1)Ordinary character - The characters representing the symbol itself are ordinary characters
2)Escape character - Symbols with special functions or special meanings are escape characters. The general format of all escape characters:\n , \nnn
    (Escape character is through\It is composed of ordinary characters, but not all ordinary characters are preceded by\Can be converted into escape characters)
    \n - Line feed
    \t - Horizontal indicator(Equivalent to pressing once Tab Key)
    \' - A single quotation mark
    \" - A double quotation mark
    \\ - A backslash
 Note: no matter how long the escape character is, it only represents a value'''
str1 = 'ab'
str2 = "abc123"
str3 = '''123'''
str4 = """m"""
# Empty string
str5 = ''

2. Character - each independent symbol of the string is a character, and the character can be any character

\u 4 Hexadecimal number of bits
# Hexadecimal number - the number on each bit can be 0-9 or a-f(A-F)
# 298235 - decimal number

3. Character coding

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

'''Common code table: ASCII Code table Unicode Coding table( python)
1)ASCII The code table has only 128 characters (not enough)
stay ASCII In the code table: numeric characters are in front of letters, uppercase letters are in front of lowercase letters, and there is a gap between uppercase letters and lowercase letters
 The number increases continuously from 0. Letter from(a/A)Start continuous increment
0(47)-9(55) A-65.. . .   a - 97.. . . . 

2)Unicode Coding table - yes ASCII Code table expansion, which includes ASCII Code table
Unicode The code table contains all symbols of all countries and nationalities in the world (universal code)
Chinese code range: 4 e00~9fa5

4. Application of coding value in python

1) chr function: chr (coded value) - the character corresponding to the coded value

2) Ord function: ord (character) - get the encoding value corresponding to the character: character - string with length of 1

3) hex - converts a number to hexadecimal

print(chr(97)) #a
print(hex(12)) #0xc

5. Common operations of string

1) Query - get character

The syntax of string acquisition character and list acquisition element is common, and the syntax of list acquisition element can be used for string acquisition

2) String related operations

a + (merge), * (repeat)

b comparison size: > < > =<=

Compare size, which is the size of the encoding of the first pair of unequal characters (compared according to the encoding of the string in the table)

Application: judge the nature of characters
a To determine whether a character is a numeric character:'0'<= x <= '9'
b Determine whether the character is lowercase:'a'<= x <= 'z'
c Determine whether the character is a capital letter:'A'<= x <= 'Z'
d Determine whether the character is a letter:'a'<= x <= 'z' or 'A'<= x <= 'Z'
e Judge whether the character is Chinese:'\u4e00'<= x <= '\u9fa5'

print('abc'>'mn') #Compare size, which is the size of the encoding of the first pair of unequal characters (compared according to the encoding of the string in the table)

c in and not in

String 1 in string 2 – judge whether string 1 is in string 2 (judge whether string 1 is a substring of string 2)

str1 = 'abc123'
print('a' in str1) #True
print('abc' in str1) #True
print('23' in str1) #True
print('13' in str1) #False judgment character must be continuous

Topics: Python