设为首页 加入收藏

TOP

python创建列表和向列表添加元素方法
2017-12-25 06:07:10 】 浏览:736
Tags:python 创建 向列表 添加 元素 方法

今天的学习内容是python中的列表的相关内容。

一.创建列表

1.创建一个普通列表

>>> tabulation1 = ['大圣','天蓬','卷帘']
>>> tabulation1
['大圣', '天蓬', '卷帘']
>>> tabulation2 = [72,36,18]
>>> tabulation2
[72, 36, 18]

2.创建一个混合列表

>>> mix tabulation = ['大圣',72,'天蓬',36]
SyntaxError: invalid syntax
>>> mixtabulation = ['大圣',72,'天蓬',36]
>>> mixtabulation
['大圣', 72, '天蓬', 36]

3.创建一个空列表

>>> empty = []
>>> empty
[]

三种方式就介绍给大家了,接下来,如果想向列表中添加元素,该怎么办呢?

二.向列表中添加元素

1.append

>>> tabulation1.append('紫霞')
>>> tabulation1
['大圣', '天蓬', '卷帘', '紫霞']

2.extend

>>> tabulation1.extend(['紫霞','青霞'])
>>> tabulation1
['大圣', '天蓬', '卷帘', '紫霞', '紫霞', '青霞']

有关于用extend拓展列表的方法,大家需要注意的是,此方法是用列表去拓展列表,而不是直接添加元素,所以“()”中要加上“[]”。

3.insert

>>> tabulation1.insert(1,'紫霞')
>>> tabulation1
['大圣', '紫霞', '天蓬', '卷帘', '紫霞', '紫霞', '青霞']

不想在原地混吃等死,所以我选择每天进步一点点。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Mongodb 3.4 + Centos6.5 配置 + .. 下一篇Python3爬虫获取

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目