这篇文章主要介绍了关于Python 创建空的list,以及append用法讲解list[1].append python,有着一定的参考价值list[1].append python,现在分享给你们,有必须的同学可以参考一下
Python中list的用法:如何构建list,如何表达list中的元素,如何设置和删除list
运行环境:Python 3.6.2
0.空list的创建:
l = list()
或者:
l = []
1.list中元素的构建跟表达
fruits = ['apple', 'banana', 'pear', 'grapes', 'pineapple', 'watermelon']
fruits[2] #从0开始数起,第三个元素
pear
2.list中元素的修改
fruits[2] = 'tomato'
print(fruits)
['apple', 'banana', 'tomato', 'grapes', 'pineapple', 'watermelon']
3.在list末尾增加更多元素
fruits.append('eggplant')
print(fruits)
['apple', 'banana', 'tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant']
4.如何截取list中的某一段
print(fruit[: 2]) #从list的首元素开始截取,截取到位置'3',但不包括第3个元素
['apple', 'banana']
5. 如何修改list中连续的元素
fruits[:2] = ['a', 'b']
print(fruits)
['a', 'b', 'tomato', 'grapes', 'pineapple', 'watermelon', 'eggplant']
6.如何删除list中某段元素,或者全部list
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-139364-1.html