标签:python 运维开发
1、列表--是一个序列,用于顺序的存储数据
列表的定义与初始化
In [374]: lst = list() In [375]: lst Out[375]: [] In [376]: lst = [] In [377]: lst = [1,2,3] In [378]: lst Out[378]: [1, 2, 3] In [379]: lst = list(range(1,10)) In [380]: lst Out[380]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
访问列表元素
In [380]: lst Out[380]: [1, 2, 3, 4, 5, 6, 7, 8, 9] In [381]: In [381]: In [381]: lst[0] Out[381]: 1 In [382]: lst[-1] Out[382]: 9 In [383]: lst.index(5) Out[383]: 4 In [384]: In [384]: lst = [1,2,3,2,4,3,5] In [385]: lst.index(2) # index 方法返回查找到的第一个索引 Out[385]: 1 In [386]: lst.index(2,2) Out[386]: 3 In [387]: lst.index(2,2,5) Out[387]: 3 In [388]: lst.index(2,-4,-1) Out[388]: 3 In [389]: lst.count(2) Out[389]: 2 In [390]: lst.count(3) Out[390]: 2
通过索引访问元素
index方法根据值返回第一个索引
count方法返回元素在列表里的个数
列表元素的修改
In [391]: lst Out[391]: [1, 2, 3, 2, 4, 3, 5] In [392]: lst[2] Out[392]: 3 In [393]: lst[2] = 5 In [394]: lst Out[394]: [1, 2, 5, 2, 4, 3, 5] In [395]: lst[2] Out[395]: 5
列表元素的增加
In [396]: lst Out[396]: [1, 2, 5, 2, 4, 3, 5] In [397]: lst.append(19) In [398]: lst Out[398]: [1, 2, 5, 2, 4, 3, 5, 19] In [399]: lst.insert(0,20) In [400]: lst Out[400]: [20, 1, 2, 5, 2, 4, 3, 5, 19] In [401]: lst.insert(-1,‘b‘) In [402]: lst Out[402]: [20, 1, 2, 5, 2, 4, 3, 5, ‘b‘, 19] In [403]: lst.insert(100,‘a‘) In [404]: lst Out[404]: [20, 1, 2, 5, 2, 4, 3, 5, ‘b‘, 19, ‘a‘] In [405]: lst.insert(-100,‘a‘) In [406]: lst Out[406]: [‘a‘, 20, 1, 2, 5, 2, 4, 3, 5, ‘b‘, 19, ‘a‘] In [407]: #insert 当索引超出范围时,索引是负数 会在第0个元素前插入,索引是正数 会在最后一个元素后插入 In [407]: lst.extend([1,2,3]) In [408]: lst Out[408]: [‘a‘, 20, 1, 2, 5, 2, 4, 3, 5, ‘b‘, 19, ‘a‘, 1, 2, 3] #append操作单个元素,extend操作可迭代对象
列表元素的删除
In [410]: lst Out[410]: [1, 2, 3, 2, 4, 3, 5, 3, 4] In [411]: lst.remove(1) In [412]: lst Out[412]: [2, 3, 2, 4, 3, 5, 3, 4] In [413]: lst.pop() Out[413]: 4 In [414]: lst Out[414]: [2, 3, 2, 4, 3, 5, 3] In [415]: lst.pop(4) Out[415]: 3 In [416]: lst Out[416]: [2, 3, 2, 4, 5, 3] In [417]: lst.clear() In [418]: lst Out[418]: []
其他操作
In [419]: lst = list(range(4)) In [420]: lst Out[420]: [0, 1, 2, 3] In [421]: len(lst) Out[421]: 4 In [422]: In [422]: lst Out[422]: [0, 1, 2, 3] In [423]: lst.reverse() In [424]: lst Out[424]: [3, 2, 1, 0] In [425]: In [425]: lst.sort() In [426]: lst Out[426]: [0, 1, 2, 3] In [427]: lst.sort(reverse=True) In [428]: lst Out[428]: [3, 2, 1, 0] In [429]: In [429]: lst Out[429]: [3, 2, 1, 0] In [430]: lst2 = lst In [431]: lst2 Out[431]: [3, 2, 1, 0] In [432]: lst2[1] = 5 In [433]: lst2 Out[433]: [3, 5, 1, 0] In [434]: lst Out[434]: [3, 5, 1, 0] In [435]: In [435]: lst2 = lst.copy() In [436]: lst2 Out[436]: [3, 5, 1, 0] In [437]: lst Out[437]: [3, 5, 1, 0] In [438]: lst2[1] = 7 In [439]: lst2 Out[439]: [3, 7, 1, 0] In [440]: lst Out[440]: [3, 5, 1, 0]
2、元组
定义及初始化
In [441]: t = tuple() In [442]: t Out[442]: () In [443]: t = () In [444]: t Out[444]: () In [445]: t = (1,2,3) In [446]: t Out[446]: (1, 2, 3) In [447]: t = tuple(range(3)) In [448]: t Out[448]: (0, 1, 2) 元组是不可变的
查询
In [449]: t Out[449]: (0, 1, 2) In [450]: t[0] Out[450]: 0 In [451]: t[-1] Out[451]: 2 In [452]: t.index(2) Out[452]: 2 In [453]: t.count(2) Out[453]: 1
命名元组
In [454]: from collections import namedtuple In [455]: User1 = namedtuple(‘A‘,[‘name‘,‘age‘]) In [456]: me = User(‘martin‘,18) In [457]: me Out[457]: A(name=‘martin‘, age=18) In [458]: me.age Out[458]: 18 In [459]: me.name Out[459]: ‘martin‘ In [460]: me[0] Out[460]: ‘martin‘ In [461]: me[1] Out[461]: 18
本文出自 “厚德载物” 博客,谢绝转载!
标签:python 运维开发
原文地址:http://huaxin.blog.51cto.com/903026/1881550