Python--2、序列常用的方法

Python-序列常用的方法

列表方法

1、增加:append,将元素添加到列表的末尾;

​ insert,添加到指定位置;

​ extend, 将序列中的每个元素追加到列表的末尾,会改变原列表。

>>> li=[4,5,6,'今天好帅']
>>> li
[4, 5, 6, '今天好帅']
>>> li.append(7)
>>> li
[4, 5, 6, '今天好帅', 7]
>>> li.insert(3,'hello')
>>> li
[4, 5, 6, 'hello', '今天好帅', 7]
>>> lt=[1,2,3,4]
>>> li+lt
[4, 5, 6, 'hello', '今天好帅', 7, 1, 2, 3, 4]
>>> li*2
[4, 5, 6, 'hello', '今天好帅', 7, 4, 5, 6, 'hello', '今天好帅', 7]
>>> li.extend(['a','b','c'])
>>> li
[4, 5, 6, 'hello', '今天好帅', 7, 'a', 'b', 'c']

2、查找:index,在列表从左到右进行查找,指定元素,返回第一个索引位置

​ index(查询元素,起始索引位置,结束索引位置)

>>> li
[4, 5, 6, 'hello', '今天好帅', 7, 'a', 'b', 'c']
>>> li.index('a')
6
>>> li=[4,6,4,4,8,9,4]
>>> li
[4, 6, 4, 4, 8, 9, 4]
>>> li.index(4)
0
>>> li.index(4,1)
2
>>> li.index(4,3,5)
3

3、count,计算指定元素出现的次数

>>> li=[4,6,4,4,8,9,4]
>>> li
[4, 6, 4, 4, 8, 9, 4]
>>> li.count(4)
4

4、help(),查看帮助文档; 、dir(list) 可查看类型的所有方法

 help(li.index)
Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.list instance
    Return first index of value.
    
    Raises ValueError if the value is not present.

5、修改:赋值语句赋值的元素原地址不变,当原始数据改变,被赋值的序列也跟着改变。可以使用

​ .copy()方法实现重新赋予地址的赋值,复制一个新的内容。

>>> a=[1,2,3]
>>> b=a
>>> id(a)
689417706752
>>> id(b)
689417706752
>>> b
[1, 2, 3]
>>> c=a.copy()
>>> c
[1, 2, 3]
>>> a.append(4)
>>> a
[1, 2, 3, 4]
>>> b
[1, 2, 3, 4]
>>> c
[1, 2, 3]
>>> id(c)
689417751616

​ 排序: .sort() ,对列表进行从小到大的排序,只支持同种类型的排序。

​ .reverse(参数,reverse=‘False’), 对列表进行从大到小的排序,只支持同种类型的排序。对序列进行排序,reverse默认False是正向排序,更改为True后,变为逆向排序

>>> a=[2,3,4,6,5,]
>>> a.sort()
>>> a
[2, 3, 4, 5, 6]
>>> a.reverse()
>>> a
[6, 5, 4, 3, 2]

6、删除:.clear() 删除所有元素,不会删除列表;

​ .pop( ) ,删除列表中索引处的元素并给出索引处的元素值。

​ .remove( ),删除所给的数据并且不会返回被删除的值。

​ del 可删除指定元素,也可删除指定变量、列表。

>>> a
[6, 5, 4, 3, 2]
>>> a.pop(1)
5
>>> a
[6, 4, 3, 2]
>>> a.remove(3)
>>> a
[6, 4, 2]
>>> a.clear()
>>> a
[]
>>> b
[1, 2, 3, 4]
>>> del b[1]
>>> b
[1, 3, 4]
>>> del b
>>> 
>>> b
Traceback (most recent call last):
  File "<pyshell#52>", line 1, in <module>
    b
NameError: name 'b' is not defined
>>> 

元组方法

1、查询: .count(value, /),返回元组中value的个数;

​ .index(value,start,stop) 返回value值所在的索引位置

>>> a=(1,2,3,)
>>> a.count(1)
1
>>> a.index(2)
1

字符串方法

1、查找

S.count(sub[, start[, end]])    返回sub 元素在字符串中出现的次数;

​ S.index(sub[, start[, end]]) 返回sub元素在字符串范围你中出现的第一个索引,若没有该元素则报错。

​ S.find(sub[, start[, end]]) 返回sub元素在字符串范围你中出现的第一个索引,若没有则返回 -1。

>>> a='hello python'
>>> a.count("o")
2
>>> a.index("e")
1
>>> a.find('python')
6
>>> a.index('p')
6
>>> a.find('j')
-1
>>> help(a.count)

2、 S.isalpha() 判断是不是全是是字母;

​ S.isdigit() 判断是不是全部是数字;

​ S.startswith( ) 判断开头元素是否相同;S.endswith() 判断结尾位置的元素是否相同。

​ S.isupper() 判断字符串是否全部为大写。

​ S.islower() 判断字符串是否全部为小写。 返回布尔类型(True、False)

3、 修改

​ .upper、把所有的字母转成大写

​ .lower、把所有的字母转成小写

​ .strip (lstrip,rstrip)、去除空格、带参数去除指定元素的开头和结尾的相同的元素.有参数是去掉两边的参数。

. capitalize、把第一个字母转换成大写

. title、把单词的首字母进行大写转换

​ .split、没有参数默认以空格来切割、有参数按照指定元素进行切割。返回一个列表。

>>> a
'hello python'
>>> a.upper()
'HELLO PYTHON'
>>> a
'hello python'
>>> b=a.upper()
>>> b
'HELLO PYTHON'
>>> b.lower()
'hello python'
>>> b
'HELLO PYTHON'
>>> a='       hello python     '
>>> a
'       hello python     '
>>> a.strip()
'hello python'
>>> a='000000hello python0000'
>>> a
'000000hello python0000'
>>> a.strip('0')
'hello python'
>>> a.lstrip('0')
'hello python0000'
>>> a='hello python'
>>> a.strip('nho')
'ello pyt'
>>> a.capitalize()
'Hello python'
>>> a.title()
'Hello Python'
>>> a.split()
['hello', 'python']

4、删除

(1)、 replace [old,new,指定修改个数] 替换把对象中有的值放第一个参数中替换成第二个参数。

>>> a='hello,    python'
>>> a
'hello,    python'
>>> a.replace(' ','')
'hello,python'
>>> a.reeplace('o','e',1)
>>> a.replace('o','e',2)
'helle,    pythen'
(2)、%s-->str   %变量名--->谁的         占位符	
>>> '%s'%a
'hello,    python'

​ 5、字符串的转义

\n 换行

\t 水平制表符

\b 退格

\r 回车,当前位置移到本行开头

\ 代表反斜杠

\’ 代表一个单引号,同样的 “ 等符号也可以这么输出

\0 代表一个空字符

\a 系统提示音

在python中如果要去掉字符串的转义,只需要在字符串前面加上 r

r’abc\tabc’

字符串编码

​ “S”.encode(encoding = UTF-8’)

​ “S”.encode(encoding=‘gbk’) 中国的标准

​ encode编码方法 encoding编码格式

decode(‘utf-8’ ) 解码

>>> b='任席伟'.encode(encoding='utf-8')
>>> b
b'\xe4\xbb\xbb\xe5\xb8\xad\xe4\xbc\x9f'
>>> b.decode('utf-8')
'任席伟'
>>> c='任席伟'.encode(encoding='gbk')
>>> c
b'\xc8\xce\xcf\xaf\xce\xb0'
>>> c.decode('gbk')
'任席伟'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值