首页 / PYTHON / python列表的用法
python列表的用法
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python列表的用法,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4013字,纯文字阅读大概需要6分钟。
内容图文
![python列表的用法](/upload/InfoBanner/zyjiaocheng/620/9f5b392e2c8b4bc79bf7129708918d09.jpg)
list内置方法
append(self, object)
用途:在list的末尾添加一个object。
返回值:返回一个添加object的list。
object表示被添加目标(可以是任意类型)
例:
test = [‘python’]
test.append(3)
test
[‘python’, 3] #输出结果
test.append([1, 2]) #添加一个列表
test
[‘python’, 3, [1, 2]] #输出结果
test.append((5, 4,)) #添加一个元组
test
[‘python’, 3, [1, 2], (4, 5)] #输出结果
test.append(2.7) #添加一个小数
test
[‘python’, 3, [1, 2], (4, 5), 2.7]
clear(self)
用途:将list清空。
返回值:返回一个空的list
例:
test = [“python”]
test
[] #输出结果,一个空的列表
copy(self)
用途:复制一份列表。
返回值:返回列表的一个副本(修改副本不会改变原有的列表)。
例:
test = [“python”]
testCopy = test.copy()
testCopy
[“python”] #输出结果
testCopy.append(3)
testCopy
[“python”, 3] #输出结果
test
[“python”] #输出结果,改变其副本,并不会改变原有列表
count(self, object)
用途:在list中统计object的个数。
返回值:返回object的个数。
其中,object表示被统计的元素(可以是任意类型)。
例:
test = [“python”]
test.count(“python”)
1 #输出结果test.append(“python”)
test.count(“python”)
2 #输出结果
extend(self, iterable)
用途:在list的末尾添加元素。(注:区分append和extend的区别)
返回值:返回在列表中添加元素后的list。
其中,iterable表示能够迭代的元素(数字不能迭代)。
例:
test = [“python”]
test.extend([1, 2, 3])
test
[‘python’, 1, 2, 3] #输出结果
test = [“python”]
test.append([1, 2, 3]) #区分extend和append
test
[‘python’, [1, 2, 3]] #输出结果
test = [“python”]
test.extend(“extend”)
test
[‘python’, ‘e’, ‘x’, ‘t’, ‘e’, ‘n’, ‘d’] #输出结果,如果是字符串,则会一个字符一个字符的迭代添加
test.extend((1,2,3,)) #添加一个元组
test
[‘python’, ‘e’, ‘x’, ‘t’, ‘e’, ‘n’, ‘d’, 1, 2, 3] #输出结果
test.extend(666) #数字不能迭代
Traceback (most recent call last):
File “<pyshell#40>”, line 1, in
test.extend(666)
TypeError: ‘int’ object is not iterable
index(self, object, start, stop)
用途:在list中查找object的索引。
返回值:返回object在list中的的位置索引。
其中,object表示需要查找的元素;
start表示list中查找的起始位置;
stop表示list中查找的末尾位置。
例:
test = [‘python’, ‘e’, ‘x’, ‘t’, ‘e’, ‘n’, ‘d’, 1, 2, 3]
test.index(“e”)
1 #输出结果,从左边至又开始查,如果有相同的元素,只会返回第一个元素的索引
test.index(“e”, 2, 9) #指定起始位置和终止位置
4
insert(self, index, object)
用途:在list中的index位置插入object元素。
返回值:返回插入元素object后的list。
其中,index表示插入元素的位置;
object表示插入的元素。
例:
test = [“python”, “insert”]
test.insert(1, [2, 3]) #在索引1处插入列表[2, 3]
test
[‘python’, [2, 3], ‘insert’] #输出结果
test.insert(2, (666, 888)) #在索引2处插入元组(666, 888)
test
[‘python’, [2, 3], (666, 888), ‘insert’] #输出结果
pop(self, index)
用途:在list中的移除index位置元素。
返回值:返回移除指定index处的元素。
其中,index表示移除元素的位置(默认为list末尾的元素索引)。
例:
test = [‘python’, [2, 3], (666, 888), ‘insert’]
test.pop()
‘insert’ #输出结果
test.pop(1)
[2, 3] #输出结果
test
[‘python’, (666, 888)] #输出结果
remove(self, object)
用途:在list中的移除object元素。
返回值:无。
其中,object表示要被移除的元素。
例:
test = [‘python’, (666, 888)]
test.remove(‘python’)
test
[(666, 888)] #输出结果
reverse(self)
用途:将list中所有的元素的进行反转。
返回值:无。
例:
test = [‘python’, ‘e’, ‘x’, ‘t’, ‘e’, ‘n’, ‘d’, 1, 2, 3]
test.reverse()
test
[3, 2, 1, ‘d’, ‘n’, ‘e’, ‘t’, ‘x’, ‘e’, ‘python’] #输出结果
sort(self, key, reverse)
用途:将list中所有的元素进行排序。
返回值:无。
其中,reverse如果为True则以逆顺进行排序,默认为False,则为正顺排序
例:
test = [3, 1, 999, 3, 5, 234]
test.sort()
test
[1, 3, 3, 5, 234, 999] #输出结果
test.sort(reverse = True) #逆顺排序
test
[999, 234, 5, 3, 3, 1] #输出结果
test = [3, 2, 1, ‘d’, ‘n’, ‘e’, ‘t’, ‘x’, ‘e’, ‘python’]
test.sort() #字符串和数字不能混合排序
Traceback (most recent call last):
File “<pyshell#61>”, line 1, in
test.sort()
TypeError: ‘<’ not supported between instances of ‘str’ and ‘int’
test = [“python”, ‘insert’]
test.sort() #对字符串进行排序
test
[‘insert’, ‘python’] #输出结果
内容总结
以上是互联网集市为您收集整理的python列表的用法全部内容,希望文章能够帮你解决python列表的用法所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。