1、python列表的特点
1)有序性。
* 方向索引
2)通过偏移来索引,从而读取数据。
* 切片
3)支持嵌套。
4)可变类型。
2、列表的常见形式
1)存储字符串
>>> a = ['1','2']>>>a
['1', '2']
2)存储数字(python3中只有int和float)
>>> b = [2,3,1]>>>b
[2, 3, 1]
3)混合
>>>c
['1', '2', 2, 3, 1]
4)列表的嵌套
>>> a= ['1','2']>>> b = [1,2]>>> e =[b,a]>>>e
[[1, 2], ['1', '2']]
>>> c = {"1":1,"2":2}>>> e =[b,a,c]>>>e
[[1, 2], ['1', '2'], {'1': 1, '2': 2}]
3、列表操作
1)定义列表
info = ['read','write','git','bash']
2)列表的增删改
* 增
在最后添加新元素
info.append("python")
结果:
['read', 'write', 'git', 'bash', 'python']
View Code
在指定索引添加新元素(列表的索引从0开始)
在索引为1的位子添加元素
info.insert(1,'cpython')
(^_0) 结果:
['read', 'cpython', 'write', 'git', 'bash', 'python']
View Code
* 删
info.remove('cpython')
(^_0) 结果:
['read', 'write', 'git', 'bash', 'python']
View Code
info.pop(0)
(^_0) 结果:
['write', 'git', 'bash', 'python']
View Code
*改
info[0] = "rewrite"
(^_0) 结果:
['rewrite', 'git', 'bash', 'python']
*查
#查看列表的所有元素
print(type(info),info)
(^_0) 结果:
['rewrite', 'git', 'bash', 'python']
View Code
#查看列表指定索引的内容
print(info[0])
(^_0) 结果:
rewrite
3)列表的其他操作
:) 列表的复制:
List1 =info.copy()print(List1)
#或者:
List2 = ['rewrite', 'git', 'bash', 'python']
print(List2)
(^_0) 结果:
['rewrite', 'git', 'bash', 'python']
:) 统计列表中某一个元素出现的次数
print(info.count('rewrite'))
(^_0) 结果:
1
:) 清空初始化列表的
info.clear()print(info)
(^_0) 结果:
[]
View Code
:) 列表的扩展
info.extend('a')
info.extend(b)
(^_0) 结果:
['rewrite', 'git', 'bash', 'python', 'a', 1, 2]
View Code
:) 取出列表中元素的索引值
info = ['read','write','git','bash']print(info.index("git"))
:) 在列表的某一个索引区间内查找元素是否存在,存在返回索引,不存在报错
info = ['read','write','git','bash']print(info.index("git",1,2))
:) 将列表中的元素反向排序
info = ['read','write','git','bash']
info.reverse()print(info)
:) 函数用于对原列表进行排序,如果指定参数,则使用比较函数指定的比较函数
List = [2,1,4,3,6,7,45,0,41]
List.sort()print(List)
或者
List = [2,1,4,3,6,7,45,0,41]print(sorted(List))
列表元素降序
List = [2,1,4,3,6,7,45,0,41]
List.sort(reverse=True)print(List)
4、列表的切割
List = [2,1,4,3,6,7,45,0,41]print(List[0:3])
List = [2,1,4,3,6,7,45,0,41]print(List[1:])
List = [2,1,4,3,6,7,45,0,41]print(List[:])
:) 设置切片的步长
List = [2,1,4,3,6,7,45,0,41]print(List[0:5:2])
5、列表操作,找中位数
List = [ x for x in range(1,50)]defFindMedian(List,FindNum):print("查找对象:",List)#获取列表的长度
try:if len(List) > 1:
mid= int(len(List)/2)if List[mid] ==FindNum:print("找到查找对象:",List[mid])elif List[mid] >FindNum:print("找的数在mid[%s]左边"%List[mid])returnFindMedian(List[0:mid],FindNum)else:print("找的数在mid[%s]右面"%List[mid])return FindMedian(List[mid+1:],FindNum)else:if List[0] ==FindNum:print("找到数字",List[0])else:print("找不到了")exceptIndexError:print("超出列表索引")
FindMedian(List,66)
6、针对本周学习遇到的问题做一个总结。
问题:同时执行多个os.system或者os.popen,解压有时能找到文件有时找不到文件。
原因:linux中运行脚本或者命令是启动一个bash,然后运行,父bash是无法继承子bash申明的环境变量,子bash是可以集成父bash的环境变量的。子bash与子bash之间的环境变量是无法相互集成的。加上python是解释执行的,当遇到os.system时,os.system在python解释为去运行当前系统命令,而当前操作系统的命令的运行过程是由操作系统决定的,python并不管这块。解释执行完一个os.system时,马上就去解释执行了下一个os.system而此时上一个os.system中的命令还在执行中。所以就有了上面的这个问题。
简单理解为一个进程起了多个线程,主进程没有等待线程执行完成就急冲冲的把线程结束了。
理解以下代码:
在bash中执行
export a=fonzie
python内容
os.system("echo $a&&export b=choosefine&&echo $b")
os.system("echo $b")
执行结果:
fonzie
choosefine
#这行为空可见我们上面的总结是正确的。
下面解决我们上面的问题:
subprocess.call()
父进程会等待子进程完成,返回退出信息(returncode),一般Linux中正常运行的返回都是0
subprocess.check_call()
父进程等待子进程完成,会自动检测运行结果是否为0,如果不为0,就会报错,错误如下:
Traceback (most recent call last):
File"modle.py", line 3, in res= subprocess.check_call(["lasdfs"])
File"/usr/lib/python3.4/subprocess.py", line 556, incheck_call
retcode= call(*popenargs, **kwargs)
File"/usr/lib/python3.4/subprocess.py", line 537, incall
with Popen(*popenargs, **kwargs) as p:
File"/usr/lib/python3.4/subprocess.py", line 859, in__init__
restore_signals, start_new_session)
File"/usr/lib/python3.4/subprocess.py", line 1457, in_execute_child
raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno2] No such file or directory: 'lasdfs'
subprocess.check_output()
父进程等待子进程完成,返回子进程标准输出到结果。
检查退出信息,如果returncode不会0,则举出错误,subprocess.CalledProcessError,该对象包含有returncode属性和output属性,output属性为标准输出的输出结果。