python 列表list初始化
2017-11-21 10:55
344 查看
python 列表list初始化
基本方法
连续数字初始化
相同的值初始化
二位数组初始化
list 练习
基本方法
$ python Python 3.5.2 (default, Sep 14 2017, 22:51:06) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> n = [1, 2, 3, 4] >>> print (n) [1, 2, 3, 4] >>>
连续数字初始化
>>> list1 = [n for n in range(1,6)] >>> print list1 File "<stdin>", line 1 print list1 #python3 特性输出要() ^ SyntaxError: Missing parentheses in call to 'print' >>> print (list1) [1, 2, 3, 4, 5] >>>
相同的值初始化
>>> #第一种方法 >>> list2 = ['a' for n in range(4)] >>> print(list2) ['a', 'a', 'a', 'a'] >>> #第二种方法:字符和数字 >>> list3 = ['b'] * 4 >>> print(list3) ['b', 'b', 'b', 'b'] >>> list4 = [3] * 4 >>> print (list4) [3, 3, 3, 3] >>>
二位数组初始化
$ python Python 3.5.2 (default, Sep 14 2017, 22:51:06) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> #初始一个5×4的数组 >>> multilist = [[1 for col in range(5)] for row in range(4)] >>> print (multilist) [[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]] >>>
list 练习
相关文章推荐
- python初始化list列表(1维、2维)
- Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
- python list(列表)操作用法总结
- python学习笔记--打印嵌套list中每个数据(遍历列表)
- python系列五:Python3列表list
- Python 列表(List)
- python之列表(list)的使用方法介绍
- python列表list,元组tuple,字典dict
- Python - 两个列表(list)组成字典(dict)
- python备忘六:list 列表之序列通用操作
- Python3学习(10)--列表生成器(List generator)
- Python 列表(List)操作方法详解
- Python学习笔记———列表(list)、元组(tuple)、字典(dict)
- 第二百九十七节,python操作redis缓存-List类型,可以理解为列表
- Python的列表操作List
- Python - 列表(list) 的 详解 及 代码
- python 学习笔记——列表(list)
- Python列表List保存对象的指针
- python中的集合详解(列表List,元组Tuple,字典,set)
- Python基础学习篇—列表(list)