From entry to prison-collections and strings

Getting started N day

set

The collection is a container data type, and each element is separated by a comma in {} as the symbol of the container

{Element 1, Element 2, Element 3...} The set is mutable and unordered, and the elements inside are immutable data, unique

<class'set'> Collection type

# 空集合
x = {
    
    }  # 这是空的字典,不是集合
x1 = set{
    
    }   #空集合

Immutable elements

s2 = {
    
    1,2,3,'13212'}
print(s2)  # {1,2,3,'13212'}

'''
s3={1,3,5,[1,2]}
print(s3)               
 # TypeError: unhashable type: 'list' 
  集合里面元素不能是列表,因为列表是可变的
'''
# 利用类型转换去重   set() 转换成集合类型   
s3=[1,2,2,3,5,10,14,10,50,50]
s4=list(set(s3))
print(s4) 

Additions, deletions, and changes to collections

Check: The collection can only be traversed, and the traversal has no order

s2={
    
    1,2,3,'231345'}
for i in s2:
    print(i)     # 通过for循环遍历都是先转换成列表,遍历列表

increase:

Collection.add(element) Add the specified element to the collection

Collection.update (sequence) Add all the elements in the sequence to the collection. The elements are immutable, otherwise an error will be reported

s2 = {
    
    1, 2, 3, '231345'}
s2.add('我是添加元素')
print(s2)  # {1, 2, 3, '我是添加元素', '231345'} 顺序不定,习惯就好
s2.update((4, 5, 6))
print(s2)  # {1, 2, 3, '我是添加元素', 4, 5, 6, '231345'}

If the added sequence is a dictionary, the added element is the key in the dictionary

s2.update({
    
    '我是key1': "vlaue", '我是key2': '1'})
print(s2)
# {1, 2, 3, 4, 5, 6, '我是key1', '231345', '我是添加元素', '我是key2'}
# 删
s2 = {
    
    1, 2, 3, '231345'}
s2.remove(1)
print(s2)  # {'231345', 2, 3}

# 改 先删除原来的元素再添加新的元素
s2 = {
    
    1, 2, 3, '231345'}
s2.remove(1)
s2.add(5)
print(s2)  # {2, 3, 5, '231345'}

The set in python supports the set operation in mathematics & (intersection) | (union),-(difference set), ^ (symmetric difference set

<> >= <= (Judging the inclusion relationship)

j1 = {
    
    1, 2, 3, 4}
j2 = {
    
    2, 3, 4, 5}
j3 = {
    
    1, 2}
j4 = {
    
    1, 2, 3, 4, 5}
print(j1 & j2)  # {2, 3, 4}  集合1 & 集合2   求两个集合的公共部分
print(j1 | j2)  # {1, 2, 3, 4, 5}   将两个集合合并
print(j1 - j2)  # {1} 去掉j1中包含在j2里面的元素
print(j2 - j1)  # {5} 去掉j2中包含在j1里面的元素
print(j1 ^ j2)  # {1, 5} 去掉两个集合公共的部分
print(j1 > j3)  # True 判断j3是否是j1的真子集

Exercise

Use three lists to indicate the names of students who choose courses in three subjects (a student can choose multiple courses at the same time)

python_student1= ['学生1', '学生2', '学生4', '学生6', '学生7', '学生10', '学生11']
h5_student2 =[ '学生1', '学生3', '学生5', '学生7', '学生8', '学生10']
java_student3 = ['学生1', '学生2', '学生5', '学生6', '学生7', '学生9', '学生12']

python_student=set(python_student1)
h5_student = set(h5_student2)
java_student =set(java_student3)
# a 求选课学生总共有多少人  12
sz=python_student | h5_student | java_student
print('有', len(sz), '个人')

# b 求只选了第一个学科的人的数量和对应的名字  {'学生11', '学生4'}
sp=python_student - h5_student - java_student
print('只选了第一个学科的学生有:', sp)

# c 求只选了一门学科的学生的数量和对应的名字 只选了一门学科的学生有 2 个 {'学生7', '学生1'}
m = python_student ^ h5_student ^ java_student
n= python_student& h5_student & java_student
s1=m-n
print('只选了一门学科的学生有', len(s1), '个', s1)

# e. 求选了三门学生的学生的数量和对应的名字
s3 = python_student & h5_student&java_student
print('选了三门的学生有:',s3)


# d. 求只选了两门学科的学生的数量和对应的名字
# 思路:用总的学生减去只选一门学科的学生和选了三门学科的学科的学生
sm=sz-s1-s3
print('只选了两门的学生有:',sm)

String

The string is the data type of the container type, and "",'','''''', """ """, as the symbol of the container

Each basic symbol inside is an element of a string

The string is immutable (addition, deletion, and modification are not supported), and the string is ordered (subscript operations are supported)

element:

Each basic unit in quotation marks is an element of a string,

Also called character (there is only the concept of characters in python, there is no corresponding type of characters)

Generally, a string of length 1 is used directly to represent characters

# 空的字符串(空串)
s1 = ''
s2 = ""
s3 = ''''''
s4 = """"""
print(type(s1), type(s2), type(s3), type(s4), s1, s2, s3, s4)  # 字符串类型

# ''、""和""""""、''''''的区别
'''
''""字符串中不能换行
’‘’‘’‘和“”“”“”就可以换行
'''
s5 = '我不能换行,不然要报错'
s6 = '''
    我 不
    可 报
    以 错
'''
print(s5, s6)

# 字符串是有序的 False
print('abc' == 'cba')

character

Characters are divided into two types: ordinary characters and escape characters

Escape character: add \ in front of one or more symbols to make the symbol have special function or special meaning

Then this kind of character with \ is an escape character

'''
\n 换行  (相当于回车)
\t 水平制表符(相当于Tab)
\' 表示一个单引号
\" 表示一个双引号
\\  表示一个反斜杠
'''

Note: The length of any escape character is 1

Characters except escape characters are ordinary characters

Ordinary characters represent the symbol itself in the string

str1 = 'abc\n123'
print(str1)
str2 = 'abc\'123'
print(str2)
str3 = "abc\"123"
print(str3)
str4 = "\"哈喽你好!\""
print(str4)
str5 = "\t我是一个反斜杠\\"
print(str5)

Special escape character-encoding character: \u followed by a four-digit hexadecimal number

Represents the character corresponding to the code value corresponding to the hexadecimal number

_str = "abc\ua222"
print(_str)  # abcꈢ
Character Encoding

The computer can only store numbers when storing data (the stored numbers are binary coded)

In order to allow the computer to store characters, each character corresponds to a fixed number

Then the fixed number corresponding to each character is the code value of this character

Character code table: The table that
records the one-to-one correspondence between characters and numbers is
the character code table. There are two common character code tables: ASCII code table and Unicode code table (python uses Unicode code table)
Unicode code table contains ASCII code table

The ASCII code table has a total of 128 characters corresponding to the code, mainly including the
universal symbols of the United States , including: English punctuation marks, numeric characters, letters and some other symbols. In the
ASCII code table, numeric characters are in front of letters, and capital letters are in Before
lowercase letters, there is a gap between uppercase and lowercase letters
A -> a There is a gap between uppercase and lowercase 32

Unicode code table
Chinese code range: 4e00~9fa5 (hexadecimal)

print('\u9ba1')  # 鮡
a = 1
# chr(编码值) 获取编码值对应的字符
print(chr(0x9ba1))  # 鮡
print(chr(0x4e00))  # 一

# 遍历编码中所有的中文
'''
for i in range(0x4e00,0x9fa5):
    print(chr(i),end='')
    a+=1
    if a % 25 ==0:
        print()
'''

# ord (字符) 获取字符对应的编码值(返回的是十进制数)
print(ord('大'), ord('爷'))  # 22823 29239
print(hex(22823), hex(29239))  # 0x5927 0x7237

# 获取字符 字符串获取字符和列表获取元素的方法一样
setence = 'you are beautiful!'
print(setence[0])  # 下标获取y
print(setence[8:-1])  # 切片获取 beautiful
print(setence[::-1])  # 反向输出 !lufituaeb era uoy

# 遍历
setence = '你好!'
for i in setence:
    print(i)
for i, j in enumerate(setence):
    print(i, j)
# 加法和乘法
print('abc' + '123456')  # abc123456 将多个字符串拼接

print('*' * 5)  # ***** 重复5次产生一个新的字符串

# 比较大小 : 两个字符串比较大小,比较第一对不相等的字符编码值的大小

print('abc' > 'ABC')  # True
print('abc' > '你好!')  # False

Judge
x'a
'<=x<='z' Judge whether it is lowercase'A
'<=x<='Z' Judge whether it is uppercase'a
'<=x<='z'or'A'<=x <='Z' Determine whether it is English
'\u4e00'<=x<='\u9fa5' Determine whether it is Chinese
'0'<=x<='9' Determine whether it is a number

# 练习 输入一个字符串,统计字符串中小写字母的个数
zfc = input('请输入:')
count = 0
for i in zfc:
    if 'a' <= i <= 'z':
        count += 1
print(count)
# 练习:输入一个字符串,删除所有的中文字符
zfc = input('请输入:')
zfc1=''
for i in zfc:
    if '\u4e00' <= i <= '\u9fa5':
        print()
    else:
        zfc1+=i

print(zfc1)

Case conversion

zfc = 'abcAB'
zfc_up = zfc.upper()
print(zfc_up)  # ABCAB
zfc_lo=zfc_up.lower()
print(zfc_lo)  # abcab

Guess you like

Origin blog.csdn.net/weixin_44628421/article/details/108873602