Python的列表
来源:互联网 发布:微信恶搞好友软件 编辑:程序博客网 时间:2024/04/29 21:40
python中的列表类似于C语言的数组(列表就是打了激素的数组)
>>> #创建列表,不需要声明类型(Python变量标识符没有类型)
>>> information= ["student0", "student1", "student2"]
>>> print(information)
['student0', 'student1', 'student2']
>>> print(information[0])
student0
>>> print(information[1])
student1
>>> print(information[2])
student2
注意:在python中双引号和单引号没有区别
列表操作
使用append()添加或pop()删除列表末尾数据选项
>>> information.append("student3")
>>> print(information)
['student0', 'student1', 'student2', 'student3']
>>> information.pop()
'student3'
>>> print(information)
['student0', 'student1', 'student2']
使用extend()在列表末尾增加一个数据集合
>>> information.extend(["student3","student4"])
>>> print(information)
['student0', 'student1', 'student2', 'student3', 'student4']
使用remove()删除或insert()增加列表一个特定位置的数据选项
>>> information.remove("student2")
>>> print(information)
['student0', 'student1', 'student3', 'student4']
>>> information.insert(2,2222)
>>> print(information)
['student0', 'student1', 2222, 'student3', 'student4']
注意:py列表可以包含混合数据
dir(list)可查看到列表的更多用法(此处暂不详述)
>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
列表迭代操作
for循环处理任意大小列表
格式:
for 目标标识符 in 列表
列表处理代码
>>> number=[0,1,2,3]
>>> for i in number:
print(i)
0
1
2
3
while循环处理任意大小列表
>>> number=[0,1,2]
>>> count = 0
>>> while count < len(number):
print(number[count])
count = count+1
0
1
2
注意:相比与C原因用{}界定代码段,python用缩进符界定。
注意:迭代处理时,能用for就不要用while,避免出现"大小差1"错误
在列表中存储列表(列表嵌套)
>>> information=['a',['b','c']]
>>> for i in information:
print(i)
a
['b', 'c']
从列表中查找列表
先之前先介绍BIF中的函数isintance(),检测某个特定标识符是否包含某个特定类型数据。(即检测列表本身识是不是列表)
>>> name=['sudent']
>>> isinstance(name,list)
True
>>> num_name=len(name)
>>> isinstance(num_name,list)
False
通过下面程序实现把列表中所有内容显示
>>> information=['a',['b','c']]
>>> for i in information:
if isinstance(i,list):
for j in i:
print(j)
else:
print(i)
a
b
c
dir(__builtins__)查看内建函数BIF有哪些
多重嵌套函数处理(使用递归函数)
>>> information=['a',['b',['c']]]
>>> def print_lol(the_list):
for i in the_list:
if isinstance(i,list):
print_lol(i)
else:
print(i)
>>> print_lol(information)
- a
- b
- c
定义函数标准格式
def 函数名(参数):
函数组代码
0 0
- python 合并列表的列表
- Python 的列表排序
- python的列表解析
- python的列表函数
- Python 的列表排序
- python列表的问题
- 列表:Python的“苦力”
- python数据结构的-----列表
- Python 的列表推导
- python的列表解析
- Python 的列表排序
- Python 的列表排序
- Python的列表相关
- python的列表(一)
- python列表的复制
- python列表的排序
- Python 列表的排序
- Python的列表
- SQL优化大全
- 线程安全集合
- C++标准库(STL)中的容器
- Crackme 5
- The First Day at School
- Python的列表
- 机器学习算法集锦
- 【backtrack】metagoofil使用方法
- Velocity快速入门教程
- MySQL优化大全
- AFN使用过程中的注意事项以及问题总结(转)
- 导入工程android build gradle project info等待很久
- java集合系列——java集合概述(一)
- Vulkan编程指南翻译 第三章 队列和命令 第1节 设备队列