Python 创建空的list,以及append用法讲解
发布时间:2020-12-16 21:06:26 所属栏目: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
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','tomato','watermelon'] 3.在list末尾增加更多元素 fruits.append('eggplant') print(fruits) ['apple','watermelon','eggplant'] 4.如何截取list中的某一段 print(fruit[: 2]) #从list的首元素开始截取,截取到位置'3',但不包括第3个元素 ['apple','banana'] 5. 如何更改list中连续的元素 fruits[:2] = ['a','b'] print(fruits) ['a','b','eggplant'] 6.如何删除list中某段元素,或者全部list fruits[:2] = [] #删除前两个元素 print(fruits) ['tomato','eggplant'] fruits[:] = [] #删除全部list元素 [] 以上这篇Python 创建空的list,以及append用法讲解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。 您可能感兴趣的文章:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |