美文网首页
序列操作

序列操作

作者: 罗泽坤 | 来源:发表于2018-10-20 21:07 被阅读0次

# 序列的操作

'''

序列的索引与反向索引

python是由C编写,序列实际上是一个对象

是一个用数组存储的字符串,故可以用数组下标来索引数据

'''

S ='luozekun'

A ='lu'

print( S[0], S[1], S[2] )

# 序列的反向索引如下,分别为倒数第一个第二个第三个元素

print( S[-1], S[-2], S[-3])

# 序列slice索引即提取出字符串的一部分

'''

一般形式为S[i:j],i为字符串数组中第i+1个元素开始偏移j个位置但不包括偏移量为J的内容

j的默认值为分别为0和分片序列的len

'''

A = S[1:3]# 取S中从第二个字符到第三个字符但不包括第三个字符的子字符串

print(A)

A = S[:4]#  第一个开始到第三个但不包括第三个

A = S[1:]# 第二个字符开始到最后一个

S[1:-1]# 第二个到倒数第二个

print(A)

'''

字符串的合并与find,replace,split,upper的使用

以及字符串的格式化

'''

# 计算序列长度

lenth =len(S)

# 用一个序列代替另一个序列的指定元素

S = S.replace('uo', 'xyz' )

print( lenth, S )

# 序列的合并

S = S +'JKL'

print(S)

#  序列分割

line ='aaa,bbb,cccc,dddddd'

line = line.split(',')# 以逗号为基准分割实际上是把数组分割然后形成新的数组

#  大小写转换

S = S.upper()

line = line[1].upper()# warning当序列split后不能够直接用序列名进行大小写转换而应该使用索引调用方法

print(S,line)

#  查找返回 -1 or 偏移量

S ='ABCK'

S = S.lower()

C = S.find('k')# 此时C 为 1

print(C)

C = S.find('O')# 此时C 为 -1

print(C)

# 判断序列是否全为字母,返回false和true

S = S +'1'

D = S.isalpha()#D此处为false

print(D)

# 格式匹配

T ='%s, dogs, and %s'%('pig','PIG')# T = pig, dogs, and PIG

print(T)

# 查看可以使用那些内置函数与内置函数的功能

print(dir(T))# 查看内置函数

print(help(T.replace))# 查看inner function function

# ord查看ascii编码

print(ord('\n'))

# 模式匹配

import re

N ='pig  \thongbifu dog'

M ='/pig/dog/monkey'

'''

搜索N中以pig开头中间有空格或者制表符以dog结尾的子序列

(.*)表示任意字符  group是一个数组对象,将(.*)匹配的字符串放入group中因为只有一个(.*)

因此只有一个group

'''

match = re.match('pig[ \t]*(.*)dog',N)

print(match.group(1))

#根据格式将pig,dog,monkey放入group中因为有三个模式匹配字符因此要用groups

match = re.match('/(.*)/(.*)/(.*)','/pig/dog/monkey')

L = match.groups()

print(L[1],L[2],L[0],L)

'''

##################################

author:Jack rose

Date:2018.10.20

Program: 序列

#总结#

在python中字符串类型即序列数据类型

实际上是一个数组对象,有方法有也有

属性,因此字符串对象可以调用各种方法

字符串是以数组的形式存储的,因此可

以通过下标索引数据。

##################################

'''

'''

代码结果:

D:\PythonProgram\venv\Scripts\python.exe D:/PythonProgram/PythonString.py

l u o

n u k

uo

uozekun

8 lxyzzekun

lxyzzekunJKL

LXYZZEKUNJKL BBB

3

-1

False

pig, dogs, and PIG

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Help on built-in function replace:

replace(old, new, count=-1, /) method of builtins.str instance

Return a copy with all occurrences of substring old replaced by new.

count

Maximum number of occurrences to replace.

-1 (the default value) means replace all occurrences.

If the optional argument count is given, only the first count occurrences are

replaced.

None

10

hongbifu

dog monkey pig ('pig', 'dog', 'monkey')

Process finished with exit code 0

'''

相关文章

  • Python序列的基本操作

    首先序列是什么: 在Python中序列有:字符串、数组、元组 切片操作 重复操作 连接操作 成员操作

  • python常用序列和基本操作

    常用序列 列表 字典 元组 常见操作 序列反转 reverse 序列排序 sort

  • 序列操作

    # 序列的操作 ''' 序列的索引与反向索引 python是由C编写,序列实际上是一个对象 是一个用数组存储的字符...

  • Python札记3_字符串基本操作

    字符串基本操作 字符串是一种序列,序列常见的公有操作: len() :求长度 + :两个序列相加 *:序列的重复,...

  • CH03序列及通用操作

    【课程3.2】 序列通用操作 序列分类:可变序列list,不可变序列tuple、str。 判断值是否属于序列 结...

  • 序列化

    关键字tansient,修饰后代表不能被默认的序列化操作,但是我们可以手动的序列化操作对子类进行反序列操作时,如果...

  • 10、for循环语句

    上集回顾: 通用序列操作 可变序列操作 连续学习了两集Python列表技能,列表(list)这个可变序列相关知识点...

  • 一些有用的内建函数

    序列产生使用range() 序列操作anyallenumeratezipsortedmap 执行其他python程...

  • 【Python】可变序列和不可变序列的通用操作

    可变序列和不可变序列的通用操作 判断值是否属于序列:in / not in 序列链接与重复:"+" "*" 下标索...

  • python数据类型之 字符串、列表、元组

    一、序列list[] 1. 序列类型操作符 成员关系操作符 in 、 not in满足成员关系返回True,...

网友评论

      本文标题:序列操作

      本文链接:https://www.haomeiwen.com/subject/ioeszftx.html