A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

字符串读取:
[AppleScript] 纯文本查看 复制代码
names=['NJ','JS','SZ','BJ','JS','LYG','JS','GZ','TG','nj','1','#']

print(names[1:2])
print(names[4])
print(names[::2])#  每隔两个元素,获取一个
print(names[1:-1])
print(names[-1:1])
print(names[1][0])
运行结果:
[AppleScript] 纯文本查看 复制代码
['JS']
JS
['NJ', 'SZ', 'JS', 'JS', 'TG', '1']
['JS', 'SZ', 'BJ', 'JS', 'LYG', 'JS', 'GZ', 'TG', 'nj', '1']
[]
J
追加&复制&统计&合并
[AppleScript] 纯文本查看 复制代码
names=['NJ','JS','SZ','BJ','JS','LYG','JS','GZ','TG','nj','1','#']
names.append(['123','456'])  #追加,默认追加到列表最后面
print(names)
#names.clear()
names_copy=names.copy() #复制一份列表
print(names_copy)
print(names.count('JS')) #统计字符串个数
names.extend(['111','222'])  #追加,合并列表
print(names)
运行结果:
[AppleScript] 纯文本查看 复制代码
['NJ', 'JS', 'SZ', 'BJ', 'JS', 'LYG', 'JS', 'GZ', 'TG', 'nj', '1', '#', ['123', '456']]
['NJ', 'JS', 'SZ', 'BJ', 'JS', 'LYG', 'JS', 'GZ', 'TG', 'nj', '1', '#', ['123', '456']]
3
['NJ', 'JS', 'SZ', 'BJ', 'JS', 'LYG', 'JS', 'GZ', 'TG', 'nj', '1', '#', ['123', '456'], '111', '222']
插入&索引&反转&排序
[AppleScript] 纯文本查看 复制代码
names.insert(2,'b') #把字符b插入到列表里,1是插入的位置
print(names)
print(names.index('LYG')) #打印LYG的下标
print(names.pop())  #默认删除最后一个
print(names.pop(2))   #指定删除位置,并打印删除的字符
names.reverse()  #反转列表
print(names)
names.remove(['123', '456'])  #移除
print(names)                          
names.sort() #给列表排序g
print(names)
运行结果:
[AppleScript] 纯文本查看 复制代码
['NJ', 'JS', 'b', 'SZ', 'BJ', 'JS', 'LYG', 'JS', 'GZ', 'TG', 'nj', '1', '#', ['123', '456'], '111', '222']
6
222
b
['111', ['123', '456'], '#', '1', 'nj', 'TG', 'GZ', 'JS', 'LYG', 'JS', 'BJ', 'SZ', 'JS', 'NJ']
['111', '#', '1', 'nj', 'TG', 'GZ', 'JS', 'LYG', 'JS', 'BJ', 'SZ', 'JS', 'NJ']
['#', '1', '111', 'BJ', 'GZ', 'JS', 'JS', 'JS', 'LYG', 'NJ', 'SZ', 'TG', 'nj']
 
拷贝番外篇:
[AppleScript] 纯文本查看 复制代码
1、copy模块
import copy  #运用模块
list_copy=['11','22','33','44',['Shenzhen',['11','22'],'Guangzhou','Changsha'],'55','66']
print(list_copy)
list_deep=copy.deepcopy(list_copy) #深copy(看下面注解)
list2=copy.copy(list_copy) #浅copy(看下面注解)
list_copy[4][1][0]='aa'
list_copy[2]='cc'
print(list_copy)
print(list_deep)
print(list2)
运行结果:
[AppleScript] 纯文本查看 复制代码
['11', '22', '33', '44', ['Shenzhen', ['11', '22'], 'Guangzhou', 'Changsha'], '55', '66'] #原始数据
['11', '22', 'cc', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66']
['11', '22', '33', '44', ['Shenzhen', ['11', '22'], 'Guangzhou', 'Changsha'], '55', '66'] #deepcopy结果
['11', '22', '33', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66'] #浅copy结果
[AppleScript] 纯文本查看 复制代码
2、copy()
list_copy=['11','22','33','44',['Shenzhen',['11','22'],'Guangzhou','Changsha'],'55','66']
list1=list_copy.copy()
print(list1)
list_copy[4][1][0]='aa'
list_copy[2]='cc'
print(list_copy)
print(list1)
运行结果:
[AppleScript] 纯文本查看 复制代码
['11', '22', '33', '44', ['Shenzhen', ['11', '22'], 'Guangzhou', 'Changsha'], '55', '66']
['11', '22', 'cc', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66']
['11', '22', '33', '44', ['Shenzhen', ['aa', '22'], 'Guangzhou', 'Changsha'], '55', '66']
注:
深copy与浅copy
共同点:不会因为列表的父层变动而变动
不同点:子列表修改时,浅copy会跟随变动而变动,而深copy不会变动。
extent()函数与append()函数区别
注:def是 python的函数,后面会介绍。
[AppleScript] 纯文本查看 复制代码
def changeextent(str):
    'Please string with extent'
    info.extend(['40','50','60']
    print('extent>>>',info)
    return
def changeappend(str):
    'Please string with append'
    info.append(['70','80','90']
    print('append>>>',info)
    return
info= ['10','20','30']
changeappend(info)
print('Please append str',info)
changeextent(info)
print('Please extent str',info)
运行结果:
[AppleScript] 纯文本查看 复制代码
append>>> ['10', '20', '30', ['70', '80', '90']]
Please append str ['10', '20', '30', ['70', '80', '90']]
extent>>> ['10', '20', '30', ['70', '80', '90'], '40', '50', '60']
Please extent str ['10', '20', '30', ['70', '80', '90'], '40', '50', '60']
结论:
1.列表可以包含任何数据类型的元素,单个列表中的元素无须全为同一个类型
2.append()函数只在尾部追加
3.列表是以类的形式实现的。“创建”列表实际上是将一个类实例化。因此,列表有多种方法可以操作。extent()方法只接受一个列表作为参数,并将该参数的每个元素都添加到原有的列表中。
字符串常用操作:
[AppleScript] 纯文本查看 复制代码
#字符串常用操作
list='abccd'
lis='!!'
print(list.count('C')) #统计字符
print(list.capitalize()) #首字母大写 等同于print('abc'.title())
print(list.center(50,'-')) # 保持字符串在中心位置,不够的用(-)补足。
# list.encode() 指定编码格式编码字符串
print(list.endswith(lis)) #用于判断字符串是否有指定后缀结尾,如果有则返回True,否则False
list_t='aaa\tbbbcccddd'
print('原始str:',list_t)    #
print('替换 \\t:' + list_t.expandtabs())  #把字符串里面\t转换为空格
print('用10空格替换 \\t:'+ list_t.expandtabs(tabsize=10)) #将Tab键,转换为10个空格
print(list_t.find('a'))  #查找字符所在位置(查下标)
print('网名:{name},网址:{url}'.format(name='baidu',url='http:///www.baidu.com'))  #格式化字符串,作用等同于%,不过format部分类型,操作更简单。
# 推荐使用,这里就不细讲了,后面会补充格式化字符串相关博客
'''
list_t.format_map() #有兴趣的自己查阅资料
list_t.index() # 检测是否包含子字符串str,如果指定beg(开始)和end(结束)范围,则检查是否包含在指定范围内,该方法与find()方法一样,只不过str不在string会报错
list_t.isdigit() #检查字符串为整数则返回 真。
list_t.isidentifier() #判断呢是不是一个合法的标示符。了解即可
list_t.isalnum() #检测字符串是否由字母和数字组成,True&False
list_t.isalpha() #检测字符串是否由字母组成,True&False
list_t.isdecimal()#检测字符串是否只包含十进制字符。这种方法只存在与unicode对象,True&False
list_t.isnumeric() #检测字符串是否由数字组成。这种方法只存在与unicode对象,True&False
list_t.isprintable() #
list_t.isspace() #检测字符串是否由空白字符组成 True&False
list_t.istitle()#检查字符串是否首写字母为大写,其他为小写  True&False
list_t.islower() #检测字符串是否由小写字母组成 True&False
list_t.isupper() #检测字符串是否由大写字母组成 True&False
'''
运行结果:
[AppleScript] 纯文本查看 复制代码
0
Abccd
----------------------abccd-----------------------
False
原始str: aaabbbcccddd
替换 \t:aaa     bbbcccddd
用10空格替换 \t:aaa       bbbcccddd
0
网名:baidu,网址:http:///www.baidu.com
 
 
list_t=('a','b','c')
print('+'.join(list_t)) #通过指定字符链接序列中元素后生成新的字符串
运行结果:
[AppleScript] 纯文本查看 复制代码
a+b+c
 
 
#.maketrans() #用于创建字符映射的转换表,对于接受的两个参数的最简单调用方式,第一个参数为字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。
#语法:
maketrans()方法语法:
str.maketrans(intab, outtab)
参数
intab -- 字符串中要替代的字符组成的字符串。
outtab -- 相应的映射字符的字符串。
 
#两个字符长度一致,必须一一对应。
intab1 = "aeiou"
outtab1 = "12345"
trantab1 = str.maketrans(intab1, outtab1) 

str = "this is string example....wow!!!"
print (str.translate(trantab1))
运行结果:
[AppleScript] 纯文本查看 复制代码
th3s 3s str3ng 2x1mpl2....w4w!!!
translate() 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符,要过滤掉的字符放到 deletechars 参数中。
语法
[AppleScript] 纯文本查看 复制代码
[mw_shl_code=applescript,true]translate()方法语法:
str.translate(table[, deletechars]); 
bytes.translate(table[, delete]) 
bytearray.translate(table[, delete]) 
[/mw_shl_code]
参数
table -- 翻译表,翻译表是通过 maketrans() 方法转换而来。
deletechars -- 字符串中要过滤的字符列表。

返回值
返回翻译后的字符串,若给出了 delete 参数,则将原来的bytes中的属于delete的字符删除,剩下的字符要按照table中给出的映射来进行映射 。
字符串补位:
[AppleScript] 纯文本查看 复制代码
list_t='abc'
print(list_t.ljust(10,'*'))  #保证字符串长度为10,不够用*号补位,补在字符串后面
print(list_t.rjust(10,'*'))  #保证字符串长度为10,不够用*号补位,补在字符串前面
运行结果:
[AppleScript] 纯文本查看 复制代码
abc*******
*******abc
字符串大小写:
[AppleScript] 纯文本查看 复制代码
list_t='abc'
list_l='EDC'
print(list_l.lower())  #字符串大写变小写
print(list_t.upper())  #字符串小写变大写
运行结果:
[AppleScript] 纯文本查看 复制代码
edc
ABC
 
移除字符:(strip 用于移除字符串两边的字符,默认移除空格)
str = "*****this is string example....wow!!!*****"
print (str.strip( '*' )) #去掉两边 *号字符
print(str.lstrip('*')) #去掉左边 *号字符
print(str.rstrip('*')) #去掉右边 *号字符
运行结果:
this is string example....wow!!!
this is string example....wow!!!*****
*****this is string example....wow!!!
切片:
[AppleScript] 纯文本查看 复制代码
print('ALex Li'.rfind('L')) # 与find相反,已右边第一个为准输出下标
print('1=2=3=4'.split('='))  #通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串
print('abc'.title()) #首字母大写
print('Ab'.swapcase())  #字母大小,反写。 大的变小,小的变大
运行结果:
[AppleScript] 纯文本查看 复制代码
5
['1', '2', '3', '4']
Abc
aB
替换:
[AppleScript] 纯文本查看 复制代码
print('Alex li'.replace('l','L',1))  #替换字符。把l替换成L。默认替换所有,后面可以指定替换个数。
运行结果:
[AppleScript] 纯文本查看 复制代码
ALex li
 
'''
.splitlines() 按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符
.startswith() 方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
.rindex() 返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间。
.replace() #replace() 方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
.zfill() # zfill() 方法返回指定长度的字符串,原字符串右对齐,前面填充0。
'''

1 个回复

倒序浏览
{:8_507:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马