当前位置: 首页 > news >正文

最好的免费发布网站百度官网首页登陆

最好的免费发布网站,百度官网首页登陆,nas做网站要哪些东东,wordpress微信图文采集python基本数据类型序列类型的自带方法1.列表的常用方法2.元祖的常用方法3.字符串的常用方法1.列表常用的方法L.append(obj) #在列表末尾添加新的对象L.clear() #清空列表L.copy() #复制列表,不是同一个对象,内容相同,有返回值。id不同(内存中…

python基本数据类型

序列类型的自带方法

1.列表的常用方法

2.元祖的常用方法

3.字符串的常用方法

1.列表常用的方法

L.append(obj) #在列表末尾添加新的对象

L.clear() #清空列表

L.copy() #复制列表,不是同一个对象,内容相同,有返回值。id不同(内存中的地址不同)

L.count(obj) #统计某个元素在列表中出现的次数

L.extend(obj) #用obj扩展原来的列表

L.index(obj) #默认值返回这个元素最先出现的索引位置;需要出现下一个位置的,添加可选参数start,stop。

Linsert(index,obj) #插入元素,可以指定位置

L.pop(index) #出栈,可以指定位置。index,默认是L[-1],按索引删除

L.remove(obj) #移除指定元素从左边开始第一个

L.reverse() #反向列表元素

L.sort() #对列表进行排序(默认ACSII)。列表中的元素要类型相同(key=len)

内置函数:

sorted()和reversed()

>>> li = [1,2,3]

>>> dir(li) #查看li列表的属性方法,带下划线的为魔法方法和私有方法,不用管。学习其它的。

['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

#那些方法对列表做了不可描述的方法

>>> help(li) #列出所有方法

>>> help(li.append) #查看li的append的用法

Help on built-in function append:

append(...) method of builtins.list instance

L.append(object) -> None -- append object to end #添加对象返回空值,在原列表的末尾添加

>>> li.append(4)

>>> li

[1, 2, 3, 4]

>>> li.clear()

>>> li

[]

>>> li = [1,2,3]

>>> id(li)

54036680

>>> li2 = li.copy()

>>> li2

[1, 2, 3]

>>> id(li2)

54636232

>>> li.count(2)

1

>>> li.append(2)

>>> li.count(2)

2

>>> li

[1, 2, 3, 2]

>>> li.extend("456")

>>> li

[1, 2, 3, 2, '4', '5', '6']

>>> li = [1,2,3]

>>> li.extend([4,5,6])

>>> li

[1, 2, 3, 4, 5, 6]

>>> li.extend((7,8))

>>> li

[1, 2, 3, 4, 5, 6, 7, 8]

>>> li

[1, 2, 3, 4, 5, 6, 7, 8]

>>> li.index(7)

6

>>> li = ['a','b','c','d']

>>> li.index('c')

2

>>> help(li.index)

Help on built-in function index:

index(...) method of builtins.list instance

L.index(value, [start, [stop]]) -> integer -- return first index of value.

Raises ValueError if the value is not present.

>>> li = ['a','b','c','d','c']

>>> li.index('c')

2

>>> li.index('c',3)

4

>>> li.index('c',3,5)

4

>>> li.index('c',3,6)

4

>>> help(li.insert)

Help on built-in function insert:

insert(...) method of builtins.list instance

L.insert(index, object) -- insert object before index

>>> li

['a', 'b', 'c', 'd', 'c']

>>> li.insert(1,'lucky')

>>> li

['a', 'lucky', 'b', 'c', 'd', 'c']

>>> li.insert(-1,'qq')

>>> li

['a', 'lucky', 'b', 'c', 'd', 'qq', 'c']

>>> help(li.pop)

Help on built-in function pop:

pop(...) method of builtins.list instance

L.pop([index]) -> item -- remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

>>> li.pop(1)

'lucky'

>>> li

['a', 'b', 'c', 'd', 'qq', 'c']

>>> li.pop()

'c'

>>> li

['a', 'b', 'c', 'd', 'qq']

>>> help(li.remove)

Help on built-in function remove:

remove(...) method of builtins.list instance

L.remove(value) -> None -- remove first occurrence of value.

Raises ValueError if the value is not present.

>>> li

['a', 'b', 'c', 'd', 'qq']

>>> li.remove('c')

>>> li

['a', 'b', 'd', 'qq']

>>> help(li.reverse)

Help on built-in function reverse:

reverse(...) method of builtins.list instance

L.reverse() -- reverse *IN PLACE*

>>> li.reverse()

>>> li

['c', 'd', 'b', 'a']

>>> help(li.sort)

Help on built-in function sort:

sort(...) method of builtins.list instance

L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*

>>> li.sort()

>>> li

['a', 'b', 'c', 'd']

>>> li = ['a','c','ab']

>>> li.sort()

>>> li

['a', 'ab', 'c']

>>> li.sort(key=len) #按照长度相等的排序然后再按不等的排序

>>> li

['a', 'c', 'ab']

>>> li.sort(key=len,reverse=True)

>>> li

['ab', 'a', 'c']

函数reversed和sorted:

>>> reversed(li) #返回一个迭代器

>>> list(reversed(li))

['c', 'a', 'ab']

>>> sorted(li)

['a', 'ab', 'c']

可变特点:

>>> li = [2,3]

>>> id(li)

53901000

>>> li.append(4)

>>> id(li)

53901000

>>> li.pop()

4

>>> id(li)

53901000

2.元祖的常用方法

count(obj) #统计某个元素在元祖中出现的次数

index(obj) #从列表中找到某个值第一个匹配项的索引位置

注意:生命只有一个元素的元组时要加逗号

特点:不可变

举例:

>>> tu = 1,2

>>> dir(tu)

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']

>>> tu.count(1)

1

>>> tu.count(3)

0

>>> help(tu.index)

Help on built-in function index:

index(...) method of builtins.tuple instance

T.index(value, [start, [stop]]) -> integer -- return first index of value.

Raises ValueError if the value is not present.

>>> tu.index(1)

0

3.字符串的常用方法

s.count(x) #返回字符串x在s中出现的次数,可带参数

s.endswith(x) #如果字符串s以x结尾,返回True

s.startswith(x) #如果字符串s以x开头,返回True

s.find(x) #返回字符串中出现x的最左端字符的索引值,如不在则返回-1

s.index(x) #返回字符串中出现x的最左端的索引值,如不在则抛出valueError异常

s.isalpha() #测试是否全为字母,都是则True;否则False

s.isdigit() #测试是否全是数字,都是则True;否则False

s.islower() #测试全是小写

s.isupper() #测试全是大写

s.lower() #将字符串转为小写

s.upper() #将字符串转为大写

s.replace(x,y) #字串替换,在字符串s中出现字符串x的任意位置都用y进行替换

s.split() #返回一系列用空格分割的字符串列表

s.split(a,b) #a,b为可选参数,a是将要分割的字符串,b是说明最多分割几个。

举例:

>>> s=''

>>> dir(s)

['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__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', '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']

>>> s.count('3')

0

>>> help(s.endswith)

Help on built-in function endswith:

endswith(...) method of builtins.str instance

S.endswith(suffix[, start[, end]]) -> bool

Return True if S ends with the specified suffix, False otherwise.

With optional start, test S beginning at that position.

With optional end, stop comparing S at that position.

suffix can also be a tuple of strings to try.

>>> s='abcde'

>>> s.endswith('a')

False

>>> s.endswith('e')

True

>>> help(s.startswith)

Help on built-in function startswith:

startswith(...) method of builtins.str instance

S.startswith(prefix[, start[, end]]) -> bool

Return True if S starts with the specified prefix, False otherwise.

With optional start, test S beginning at that position.

With optional end, stop comparing S at that position.

prefix can also be a tuple of strings to try.

>>> s.startswith('a')

True

>>> help(s.find)

Help on built-in function find:

find(...) method of builtins.str instance

S.find(sub[, start[, end]]) -> int

Return the lowest index in S where substring sub is found,

such that sub is contained within S[start:end]. Optional

arguments start and end are interpreted as in slice notation.

Return -1 on failure.

>>> s.find('v') #不存在的值

-1

>>> s.find('b')

1

>>> help(s.index)

Help on built-in function index:

index(...) method of builtins.str instance

S.index(sub[, start[, end]]) -> int

Like S.find() but raise ValueError when the substring is not found.

>>> s.index('e') #默认返回最先出现的位置

4

>>> s='abcde'

>>> s.isalpha() #测试全是字母

True

>>> s.isdigit() #测试全是数字,只能测试正整数,负数返回-1

False

>>> s.islower() #测试全是小写

True

>>> s.isupper() #测试全是大写

False

>>> s1 = '12AA'

>>> s1.isupper()

True

>>> s1.upper() #全部改成大写

'12AA'

>>> s1.lower() #全部改成小写

'12aa

>>> help(s.replace)

Help on built-in function replace:

replace(...) method of builtins.str instance

S.replace(old, new[, count]) -> str

Return a copy of S with all occurrences of substring

old replaced by new. If the optional argument count is

given, only the first count occurrences are replaced.

>>> s1.replace('12','bb') #将旧元素替换成新的元素。替换次数的参数可选

'bbAA'

>>> s1.replace('A','b',1)

'12bA'

>>> help(s.split)

Help on built-in function split:

split(...) method of builtins.str instance

S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the

delimiter string. If maxsplit is given, at most maxsplit

splits are done. If sep is not specified or is None, any

whitespace string is a separator and empty strings are

removed from the result.

>>> s.split(sep='o')

['i l', 've pyth', 'n']

>>> s.split(maxsplit=1)

['i', 'love python']

>>> s.split('1')

['i love python']

>>> s.split(sep='o',maxsplit=1)

['i l', 've python']

>>> s='123'

>>> s.split(sep='1')

['', '23']

http://www.lbrq.cn/news/2798641.html

相关文章:

  • 丹东建设工程信息网站html简单网页设计作品
  • 网站建设在线推广推广代理公司
  • 投票网站设计百度官网app下载
  • 游戏网页代码西安百度seo
  • 宁波哪家公司做网站好百度云盘登录入口
  • 响应式网站是怎么做的seo的基本内容
  • 建站节沈阳黄页88企业名录
  • 西安做门户网站最好的公司运营推广
  • 珠海市住房城乡建设官网北京seo公司wyhseo
  • 成都app制作软件seo教程免费
  • 建设摩托车是名牌吗关键词优化到首页怎么做到的
  • 靠谱网站优化哪家好交换友链
  • 网页设计需求模板seo营销服务
  • 住房新建网站在线crm系统
  • 医院网站建设方案书360指数在线查询
  • wordpress做学校网站合肥网站排名推广
  • 我先做个网站怎么做的百度快速排名优化技术
  • 怎样制作微信网站优化设计答案五年级上册
  • 建站的步骤有哪些在线培训网站
  • 网站建设需要注意哪些百度优化教程
  • 微信显示wordpress南京seo外包
  • 北京城市建设档案馆网站网站优化seo怎么做
  • dw怎么做网站首页广州seo排名优化服务
  • 赣州建站一手app推广接单平台
  • 东西湖区网站建设公司网络营销策划需要包括哪些内容
  • 一站式企业服务提高工作效率
  • 公司网站建设方案模板下载郑州网络推广方案
  • 成都怎样制作公司网站四川网络推广seo
  • 开发公司 网站建设kol合作推广
  • 做3d打印网站百度竞价ocpc
  • 迅为R3568开发板OpeHarmony学习开发手册-配置远程访问环境
  • 打破传统课程模式,IP变现的创新玩法 | 创客匠人
  • linux-ubuntu里docker的容器portainer容器建立后如何打开?
  • Advanced Math Math Analysis |02 Limits
  • 算法 ----- 链式
  • RK-Android11-PackageInstaller安装器自动安装功能实现