Python中的list为python中的常用数据类型,其为python中内建类,继承自object。接下来全面介绍list的常见方法及自己实现类Python list功能的类
定义list>>> li = ["a", "b", "mpilgrim", "z", "example"]
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[0]
'a'
>>> li[4]
'example'
负的list 索引>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[-1]
'example'
>>> li[-3]
'mpilgrim'
>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li[1:3]
['b', 'mpilgrim']
>>> li[1:-1]
['b', 'mpilgrim', 'z']
>>> li[0:3]
['a', 'b', 'mpilgrim']
向 list 中增加元素>>> li
['a', 'b', 'mpilgrim', 'z', 'example']
>>> li.append("new")
>>> li
['a', 'b', 'mpilgrim', 'z', 'example', 'new']
>>> li.insert(2, "new")
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new']
>>> li.extend(["two", "elements"])
>>> li
['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
《Python中list的含义是什么?一文搞懂Python中list的方法和用法》总结了关于学习编程教程,对于我们来电脑技术002pc网确实能学到不少知识。遍历list1 = [x for x in range(0,10,2)]
# 方法一
for i in range(len(list1)):
print(list1[i], end=' ')
# 方法二
for x in list1:
print(x, end=' ')
# 方法三
for ind,value in enumerate(list1):
print(ind, value, sep='=', end = ' ')
以上为个人理解,如有误解或错误,请指正
以上就是Python中list的含义是什么?一文搞懂Python中list的方法和用法的详细内容,更多请关注第二电脑网其它相关文章!
更多:Python中list的含义是什么?一文搞懂Python中list的方法和用法
https://www.002pc.comhttps://www.002pc.com/python/2351.html
你可能感兴趣的Python,list,搞懂,用法,一文,含义
No alive nodes found in your cluster
0踩
赏
0 赞