码迷,mamicode.com
首页 > 其他好文 > 详细

4-列表方法

时间:2020-05-16 10:37:10      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:license   more   字母   isl   edits   string   查找   %s   copy   

lesson04列表方法:
视频1:列表的方法:
看ppt:


sudo pip3 install ipython3 //安装ipython3

//ipython3
pyvip@Vip:~$ ipython3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython‘s features.
%quickref -> Quick reference.
help -> Python‘s own help system.
object? -> Details about ‘object‘, use ‘object??‘ for extra details.

In [1]: li=[1,2,3]

In [2]: li
Out[2]: [1, 2, 3]

In [3]:

列表的 增、查、删、改、其他复制,反向,排序
>>>
>>> li=[1,2,3]
>>> li
[1, 2, 3]
>>> li.append(11) //尾部插入
>>> li
[1, 2, 3, 11]
>>> li.insert(2,22) //指定插入
>>> li
[1, 2, 22, 3, 11]
>>> li.extend([66,77,88]) //插入多个元素,可以是字符串,列表,元祖,一般都用列表
>>> li
[1, 2, 22, 3, 11, 66, 77, 88]
>>>
>>> li.index(2) //返回第一个数值为2的下标
1
>>> li.count(2) //计数
1
>>>
>>> li.pop() //尾部删
88
>>> li
[1, 2, 22, 3, 11, 66, 77]
>>> li.pop(1) //下标删
2
>>> li
[1, 22, 3, 11, 66, 77]
>>> li.remove(66) //指定数值删
>>> li
[1, 22, 3, 11, 77]
>>> li.clear() //清空列表
>>> li
[]
>>>
>>> li=[1,2,3,1,2,3]
>>> li.copy()
[1, 2, 3, 1, 2, 3]
>>> li.reverse() //列表数值反转
>>> li
[3, 2, 1, 3, 2, 1]
>>> li.sort() //列表排序
>>> li
[1, 1, 2, 2, 3, 3]
>>>

地址
>>> a=li.copy()
>>> b=li
>>> a
[1, 1, 2, 2, 3, 3]
>>> b
[1, 1, 2, 2, 3, 3]
>>> li
[1, 1, 2, 2, 3, 3]
>>> id(a)
3072141004
>>> id(b)
3072140812
>>> id(li)
3072140812
>>>

//列表排序用法
>>> li=[1,2,44,55,66,33]
>>> li.sort(reverse=True)
>>> li
[66, 55, 44, 33, 2, 1]
>>> li.sort(reverse=False)
>>> li
[1, 2, 33, 44, 55, 66]
>>>

------------------------------------------------------------
//视频2:字符串的方法
//字符串的查找,分割,替换
>>> str1=‘123aa123bb123cc123‘
>>> str1
‘123aa123bb123cc123‘
>>> str1.count(‘a‘)
2
>>> str1.find(‘a‘)
3
>>> str1.find(‘d‘) //没找到返回-1
-1
>>>
>>> str1.index(‘a‘)
3
>>> str1.index(‘d‘) //index和find一样,返回数值下标,没找到返回错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found

>>> str1.split(‘1‘) //‘1’位置分割
[‘‘, ‘23aa‘, ‘23bb‘, ‘23cc‘, ‘23‘]
>>> str1.split(‘1‘,2) //‘1’位置分割前2份
[‘‘, ‘23aa‘, ‘23bb123cc123‘]
>>>
>>> a=str1.replace(‘1‘,‘9‘) //替换
>>> a
‘923aa923bb923cc923‘
>>> str1 //原始不变
‘123aa123bb123cc123‘
>>>


//字符串方法
>>> str1
‘123aa123bb123cc123‘
>>> str1.endswith(‘23‘) //查看字符串是否指定字段结束
True
>>> str1.startswitch(‘123aa‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: ‘str‘ object has no attribute ‘startswitch‘
>>> str1.isdigit() //是否纯数字
False
>>> str1.isalpha() //是否纯字母
False
>>> str1.islower() //是否小写
True
>>> str1.isupper() //是否大写
False
>>> str1.upper() //转大写
‘123AA123BB123CC123‘
>>> str1.lower() //转小写
‘123aa123bb123cc123‘
>>> str1.capitalize() //字符串第一个字母转大写
‘123aa123bb123cc123‘
>>> str1.title() //每个单词首字母大写
‘123Aa123Bb123Cc123‘
>>> str2=‘ abc ‘
>>> str2.strip() //删除2端空白字符
‘abc‘
>>> str2
‘ abc ‘
>>> str2.lstrip() //删除左边空白字符
‘abc ‘
>>> str2.rstrip() //删除右边空白字符
‘ abc‘
>>>

 

 

------------------------------------------------------------
//视频3 字符串拼接 4种
>>>
>>> ‘11‘+‘22‘+‘python‘
‘1122python‘
>>> ‘%s%s1111111%s‘%(‘abc‘,‘dg‘,‘bv‘)
‘abcdg1111111bv‘
>>> ‘1111
File "<stdin>", line 1
‘1111
^
SyntaxError: EOL while scanning string literal
>>> ‘1111‘.join(‘abc‘,‘dgg‘,‘cz‘)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: join() takes exactly one argument (3 given)
>>> ‘11‘.join([‘a‘,‘b‘,‘z‘])
‘a11b11z‘
>>> ‘{}000000{}----{}‘.format(‘abc‘,‘gds‘,‘zz‘)
‘abc000000gds----zz‘
>>> ‘{2}{1}{0}‘.format(‘hello‘,‘py‘,‘nihao‘)
‘nihaopyhello‘
>>> ‘{n0}{n2}{n1}‘.format(n1=‘asd‘,n0=‘111‘,n2=‘pthon‘)
‘111pthonasd‘
>>>

------------------------------------------------------------
//视频4 元祖的方法
查,count index
>>> tu=(1,2,3,4,1,2,3,1,2)
>>> tu.count(1)
3
>>> tu.count(2)
3
>>> tu.count(3)
2
>>> tu.count(4)
1
>>> tu.count(5)
0
>>> tu.index(4) //返回找到数值的下标
3
>>> tu.index(33)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple
>>>

 

4-列表方法

标签:license   more   字母   isl   edits   string   查找   %s   copy   

原文地址:https://www.cnblogs.com/tiantiancode/p/12899001.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!