找回密码
 立即注册

推荐阅读

  • 便民服务
  • 关注我们
  • 社区新手
python语言中常见的数据结构:字符串(str)、整形(int)、浮点型(fioat)、字典(dict)、元组(tuple)、集合(set)
1.索引(index):表示的是数据结构中各个元素所占的位置:分为正向索引和负向索引
正向索引:从数据结构的左边往右数,且从数字0开始
负向索引:从数据结构的右边往左数,且从数字-1开始
负向索引:-6     -5     -4     -3     -2     -1
str1 =         'a       b      c      d       e      f'
正向索引: 0       1      2      3       4     5
2.切片
序列:指的是python中有顺序的数据结构
定义:切片是指对操作的对象截取其中一部分的操作,字符串、列表、元组都支持,即是python中的序列都支持索引和切片
索引和切片
切片的语法:
[start_index:end_index : step]
解释说明:
start_index表示起始索引
end_index表示结束索引
step表示步长,步长不能为0,且默认值为1
注意:
1.切片操作是指按照步长,截取从起始索引到结束索引,但不包含结束索引(也就是结束索引减1)的所有元素
2.当start_index和end_index不写时,表示无穷大或者无穷小
str1 = 'asdfghjkl'
print(str1[1])                              执行结果:s    注意:该写法不表示切片,而是表示根据索引位取值
print(str1[0:5:1])                        执行结果:asdfg
print(str1[1:6:2])                        执行结果:sfh
print(str1[-7:5])                          执行结果:dfg
print(str1[0:])                             执行结果:asdfghjkl
print(str1[::-1])                           执行结果:lkjhgfdsa  反转输出
print(str1[0::-1])                         执行结果:a
print(str1[-1:1:1])                       执行结果:空
print(str1[3:-1:-2])                      执行结果:空
print(str1[:-2])                            执行结果:asdfghj   

python中字符串常用的函数
1.根据索引访问字符串中的某个值
str1 = 'hello_duoceshi'
print(str1[3])                执行结果:1
2.把字符串的手写字母变为大写:capitalize()
print(str1.capitalize())                 执行结果:Hello_duoceshi
print(str1.title)                             执行结果:Hello_Duoceshi
3.把字符串的全部字母变成大写:upper()
print(str1.upper())                       执行结果:HELLO_DUOCESHI
4.把字符串的全部字母变成小写:lower()
str2 = 'HELLO_DUOCESHI'
print(str2.lower())                        执行结果:hello_duoceshi
s = str2.lower()
print(s)
5.统计字符串中某个字符/字符串出现的次数:count()
str1 = 'hello_duoceshi'
print(str1.count('h'))               执行结果:2
print(str1.count('ll'))               执行结果:1
6.字符串合并:join()
str1 = ' hello'  
str2 = 'dcs'
print(str1.join(str2))               执行结果:dhellochellos
print(str2.join(str1))               执行结果:hdcsedcsldcso
7.删除字符串中首位特定的字符/字符串
print(str1.strip('he'))              执行结果:llo_duoceshi
8.单独删除字符串中起始/结束位置的字符/字符串:lstrip()/rstrip()
print(str1.lstrip('h'))               执行结果:ello_duoceshi
print(str1.rstrip('hi'))              执行结果:hello_duoces
9.把字符串按照某种特定的符号/字符串进行切割:split()     返回的结果是一个列表
str1 = 'hello_duoceshi'
print(str1.split('_'))                 执行结果:['hello','duoceshi']
print(type(str1.split('_')))        执行结果:
10.查找字符串中某个字符/字符串的位置(索引值):find()
str1 = 'hello_duoceshi'
print(str1.find('h'))                   执行结果:0
11.从右往左查找字符串中某个字符/字符串的位置:rfind()
print(str1.rfind('h'))                  执行结果:12
12.替换字符串中的某个字符/字符串replace()
print(str1.replace('_','.'))          执行结果:hello.duoceshi
print(str1.replace("h","d"))       执行结果:dello_duocesdi
指定次数替换
print(str1.replace('h','d',1))       执行结果:dello_duoceshi
13.判断字符串是否以某个字符/字符串开头/结尾:starswith()/endswith()
srt1 = 'hello_duoceshi'
print(str1.startswith('h'))           执行结果:True
print(str1.startswith('e'))           执行结果:False
print(str1.startswith('he'))         执行结果:True
print(str1.startswith('hw'))         执行结果:False
print(str1.endswith('i'))              执行结果:True
print(str1.endswith('a'))             执行结果:False
14.判断字符串是否全为数字:isdigit()
num = '123456'
print(num.isdigit())                     执行结果:True
15.判断字符串是否为全字母:
str1 = 'hello'
print(str1.isalpha())                     执行结果:True
16.判断字符串是否为字符和数字组合:
str1 = 'hellosh18'
print(str1.isalnum())                    执行结果:True
17.判断字符串是否全为大/小写字符:isupper()/islower()
str1 = 'HELLO'
str2 = 'hello'
print(str1.isupper())                    执行结果:True
print(str2.isupper())                    执行结果:False
print(str1.islower())                     执行结果:False
print(str2.islower())                      执行结果:True
18.判断字符串是否以大写字母开头:istitle()
str1 = 'Hello'
str2 = 'hello'
print(str1.istitle())                          执行结果:True
print(str2.istitle())                          执行结果:False

需求:有两个字符串如str1 = 'hello' 和str2 = 'cuoceshi',要求处理成如:HelloDuoceshi样式
str1 = 'hello'
str2 = 'duoceshi'
a = str1.capitalize()
b = str2.capitalize()
print(a+b)

python中的列表:list[]
定义列表的方法:
1.直接使用[]定义列表
list1 = [1,2,3,1,'test','python']
print(type(list1))                            执行结果:
2.使用list方法把字符串转换成列表
str1 = 'hello'
list2 = list(str1)
print(list2)                                     执行结果:['h','e','l','l','o']
print(type(list2))                            执行结果:
3.使用[]定义空列表
list3 = []
print(list3)                                     执行结果:[]
print(type(list3))                            执行结果:

列表常用的函数
1.根据索引取值、赋值、切片
list = [1,2,3,1,'test',python]
print(list1[2])                                  执行结果:3
print(list1[1:-1:2])                           执行结果:[2,1]
list1[3] = 'dcs'
print(list1)                                       执行结果:[1,2,3,'dcs','test','python']
2.往列表的最后位置添加元素:apppend()、extend()、   默认添加在列表的最后位置
list1 = [1,2,3,1,'test','python']
list1.append('dcs')
print(list1)                                         执行结果:[1,2,3,1,'test','python','dcs']
list2 = ['dcs','shanghai']
list1.append(list2)
print(list1)                                         执行结果:[1,2,3,1,'test','python','dcs',['dcs','shanghai']]
print(type(list1))                                执行结果:
list1.extend('a')
print(list1)                                         执行结果:[1,2,3,1,'test','python','a']
list3 = ["dcs", 'shanghai']
list1.extend(list3)
print(list1)                                          执行结果:[1,2,3,1,'test','python','a','dcs','shanghai']
3.在列表的指定位置添加元素:insert()
list1 = [1,2,3,1,'test','python']
list1.insert(4,'dcs')
print(list1)                                          执行结果:[1,2,3,1,'dcs','test','python']
4.指定删除列表中的元素:remove()
print(list1.remove('dcs'))
print(list1)                                          执行结果:[1,2,3,1,'test','python']
直接使用del方法删除
del list1[3]
perin(list1)                                          执行结果:[1,2,3,1,'test','python']
5.随机删除列表中的元素:pop()     一般默认删除列表的最后一个元素
list1 = [1,2,3,1,'test','python']
print(list1.pop())                                  执行结果:python
print(list1)                                           执行结果:[1,2,3,1,'test']
注意:pop()函数是列表中唯一一个具有返回的函数,也即是可以把删除的元素打印出来
6.根据元素的值查找对应的索引位:index()
list1 = [1,2,3,1,'test','python']
print(list1.index('test'))                        执行结果:4
print(list1.index('test',1,5))             执行结果:4
7.排序显示列表的值:sort()
list1 = [5,11,2,3,6,40]
list1.sort()
print(list2)                                            执行结果:[2,3,5,6,11,40]
8.降序排列显示列表的值
list1 = [5,11,2,3,6,40]
list2 = sorted(list1,reversr=True)
print(list2)                                            执行结果:[40,11,6,5,3,2]
9.反转输出
list1.reverse()
print(list1)                                            执行结果:[40,6,3,2,11,5]

python中的元组  tuple()
元组的定义方法:
1.直接使用()定义元组
tuple1 = (1,2,3,22,"test")
print(type(tuple1))                                 执行结果:
print(tuple1)                                           执行结果:(1,2,3,22,"test")
2.使用tuple()函数将列表转换成元组
list1 = [1,2,3,11,'test']
tuple = tuple(list1)
print(tuple1)                                           执行结果:(1,2,3,11,'test')
print(tpe(tuple1))                                    执行结果:
3.当元组中只有一个元素时,需要在元素后添加一个逗号,否则该序列不是一个元组
tuple1 = ('test',)
print(type(tuple1))

元组可执行操作:索引取值、切片
tuple1 = (1,2,3,22,'test')
print(tuple1[2])                                        执行结果:3
print(tuple1[1:4:2])                                  执行结果:(2,22)

面试题
1.元组中的值可以改变吗?
不可以。如果真要改变元组的元素值,只能通过间接方法把元组转换成列表,修改元素值后再转换成元组
2.元组和列表相比较,哪一个序列处理数据的速度更快?
    元组

元组和列表之间的转换
tuple1 = (1,2,3,22,'test')
list1 = list(tuple1)                使用list函数把元组转换成列表
print(list1)
list1[2] = 'dcs'
print(list1)
tuple1 = tuple(list1)             使用tuple函数把列表转换成元组
print(tuple1)                         执行结果:(1,2,'dcs',22,'test')
print(type(tuple1))                执行结果:         

python中的集合:可变集合(set)、不可变集合(frozenset)
作用:主要用于去重

可变集合
list1 = [1,2,3,1,11,22,'test','test']
set1 = set(list1)
print(set1)
print(type(set1))                          执行结果:
print(set1.pop())                          pop()函数在集合中默认删除的是集合中的第一个元素
set1.remove('test')
print(set1)                                   执行结果:{1,2,3,11,22}
set1.add('dcs')
print(set1)                                   执行结果:{1,2,3,11,'test',22,'dcs'}
set1.clean()                                清除集合中的所有元素
print(set1)                                   执行结果:set()
set1.update('dcs')
print(set1)                                   执行结果:{1,2,3,11,'test','c',22,'d','s'}
不可变集合
str1 = 'helloduoceshi'
set1 frozenset(set1)
print(set1)                                    执行结果:frozenset{'c','i','s','u','e','o','d','l','h'}
set1.add("dcs")
print(set1)                                    执行结果:AttributeError:'frozenset' object has no attribute 'add'




分享至 : QQ空间
收藏

0 个回复

您需要登录后才可以回帖 登录 | 立即注册