自动化运维Python系列之基础数据类型
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动化运维Python系列之基础数据类型相关的知识,希望对你有一定的参考价值。
一、列表和元组
列表是我们最常用到的数据类型之一,在一个变量中定义多个变量,可以理解为数组
定义列表
>>> name = ["liunx01","linux02","Unix",22] >>> print(name) [‘liunx01‘, ‘linux02‘, ‘Unix‘, 22] >>> name[0] ‘liunx01‘ >>> name[-1]
步长
>>> name [‘liunx01‘, ‘linux02‘, ‘Unix‘, 29, 22] >>> name[0::2] [‘liunx01‘, ‘Unix‘, 22]
切片,取多个元素
>>> name[0:1] [‘liunx01‘] >>> name[0:2] [‘liunx01‘, ‘linux02‘] >>> name[-3:-1] [‘linux02‘, ‘Unix‘] >>> name[-3:] [‘linux02‘, ‘Unix‘, 22] >>> name[:3] [‘liunx01‘, ‘linux02‘, ‘Unix‘] >>> name[:3][:2] [‘liunx01‘, ‘linux02‘]
追加
>>> name.append("CentOS") >>> name [‘liunx01‘, ‘Unix‘, 22,‘CentOS‘]
插入
>>> name.insert(2,‘RetHat‘) >>> name [‘liunx01‘, ‘Unix‘,‘RetHat‘,22,‘CentOS‘]
修改
>>> name[1] = "CentOS" >>> name [‘liunx01‘, ‘CentOS‘, ‘RetHat‘, 22,‘CentOS‘]
删除
>>> name [‘liunx01‘, ‘CentOS‘, ‘RetHat‘, ‘Unix‘, 22] >>> name.remove("RetHat") #指定元素删除 >>> del name[0:2] #切片删除 >>> name.pop() #删除最后一个value >>> name [‘Unix‘]
扩展
>>> name [‘liunx01‘, ‘CentOS‘, ‘RetHat‘, ‘Unix‘, 22] >>> b = [1,2,3] >>> name.extend(b) >>> name [‘liunx01‘, ‘CentOS‘, ‘RetHat‘, ‘Unix‘, 22, 1, 2, 3]
统计
>>> num = name.count(22) >>> print(num) 1
倒序、排序
>>> name = [22,"linux01","linux02","Unix",22] >>> name.reverse() >>> print(name) [22, ‘Unix‘, ‘linux02‘, ‘linux01‘, 22] >>> name = [22,"linux01","linux02","Unix",22] >>> name.sort() #注意3.0里面字符串和数字不能直接排序,用2.0 >>> print(name) [22, 22, ‘Unix‘, ‘linux01‘, ‘linux02‘]
找索引位置
name = [22,"linux01","linux02","Unix",22] for i in range(name.count(22)): ele_index = name.index(22) name[ele_index] = 999 print(name)
判断列表中是否存在一个元素
>>> name [‘liunx01‘, ‘linux02‘, ‘Unix‘, 29, 22] >>> print(2 in name) False >>> print(22 in name) True
拷贝
>>> name [‘liunx01‘, ‘CentOS‘, ‘RetHat‘, ‘Unix‘, 22] >>> name_copy = name.copy() >>> name_copy [‘liunx01‘, ‘CentOS‘, ‘RetHat‘, ‘Unix‘, 22]
列表的拷贝可不止上面那么简单:
共享列表第二层内存地址
import copy name = [22,"linux01","linux02",[88,87,86],"Unix",22] name3 = name.copy() #拷贝列表 name4 = copy.copy(name) #浅copy(软链接) name5 = copy.deepcopy(name) #深copy(完全拷贝) name[0] = 100 name[3][2] = 100 print(name) print(name3) print(name4) print(name5) #输出 [100, ‘linux01‘, ‘linux02‘, [88, 87, 100], ‘Unix‘, 22] [22, ‘linux01‘, ‘linux02‘, [88, 87, 100], ‘Unix‘, 22] [22, ‘linux01‘, ‘linux02‘, [88, 87, 100], ‘Unix‘, 22] [22, ‘linux01‘, ‘linux02‘, [88, 87, 86], ‘Unix‘, 22]
元组
元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表
names = ("alex","jack","eric")
它只有2个方法,一个是count,一个是index
字符串操作
n_arg = {‘name‘:‘lichengbing‘,‘age‘:25}n = ‘My name is {name} and age is {age}‘ print(n.format_map(n_arg)) My name is lichengbing and age is 25 n1 = ‘Hello World‘ print(n1.ljust(40,"-")) Hello World----------------------------- n1 = ‘Hello World‘ print(n1.rjust(40,"-")) -----------------------------Hello World s = "Hello World!"p = str.maketrans("abcdefg","[email protected]#$%^") print(s.translate(p)) H$llo Worl#! b="ddefdsdff_哈哈" print(b.isidentifier()) #检测一段字符串可否被当作标志符,即是否符合变量命名规则 True
字典
由于列表中嵌套列表的做法不便于我们取值,所以就有了字典
字典的特性:key-value、dict是无序的、key值必须唯一
字典的定义
id_db = { 100: { ‘name‘:"linux", ‘age‘:24, ‘addr‘:"shanghai" }, 101: { ‘name‘:"nuix", ‘age‘:23, ‘addr‘:"anhui", }, } 输出 {100: {‘age‘: 24, ‘name‘: ‘linux‘, ‘addr‘: ‘shanghai‘}, 101: {‘age‘: 23, ‘name‘: ‘nuix‘, ‘addr‘: ‘anhui‘}}
增加(或者覆盖)
dict2 = { ‘name‘: "Redhat", 103: { ‘name‘:"Ubuntu", }, } id_db.update(dict2) print(id_db) {‘name‘: ‘Redhat‘, 100: {‘age‘: 24, ‘name‘: ‘linux‘, ‘addr‘: ‘shanghai‘}, 101: {‘age‘: 23, ‘name‘: ‘nuix‘, ‘addr‘: ‘anhui‘}, 103: {‘name‘: ‘Ubuntu‘}}
删除
del id_db[101] #删除整个value里面的数据 id_db[101].pop("addr") #删除单个数据
获取get
v = id_db.get(102) print(v)
获取值或者key
print(id_db.values()) print(id_db.keys()) dict_values([{‘name‘: ‘linux‘, ‘addr‘: ‘shanghai‘, ‘age‘: 24}, {‘name‘: ‘nuix‘, ‘addr‘: ‘anhui‘, ‘age‘: 23}]) dict_keys([100, 101])
字典转列表
print(id_db.items()) dict_items([(100, {‘name‘: ‘linux‘, ‘addr‘: ‘shanghai‘, ‘age‘: 24}), (101, {‘name‘: ‘nuix‘, ‘addr‘: ‘anhui‘, ‘age‘: 23})])
判断包含
id_db.has_key(185185) #only in 2.x if 185185 in id_db: print("yes") else: print("no")
setdefault
print(id_db.setdefault(185,"9999")) #取一个值,如果值不存在则新添加一个默认值 print(id_db) 9999 {185: ‘9999‘, 100: {‘addr‘: ‘shanghai‘, ‘age‘: 24, ‘name‘: ‘linux‘}, 101: {‘addr‘: ‘anhui‘, ‘age‘: 23, ‘name‘: ‘nuix‘}}
fromkeys
print(id_db.fromkeys([1,2,3,56],‘dddd‘)) #设置一个新字典,前面是key后面填充value print(id_db) {56: ‘dddd‘, 1: ‘dddd‘, 2: ‘dddd‘, 3: ‘dddd‘} {100: {‘name‘: ‘linux‘, ‘addr‘: ‘shanghai‘, ‘age‘: 24}, 101: {‘name‘: ‘nuix‘, ‘addr‘: ‘anhui‘, ‘age‘: 23}}
popitem
print(id_db.popitem()) #随机删除一个key,不要用这个随机删除 (100, {‘name‘: ‘linux‘, ‘age‘: 24, ‘addr‘: ‘shanghai‘})
循环
‘‘‘ for k,v in id_db.items(): #循环效率低,因为有一个dict转list的过程 print(k,v) ‘‘‘ for key in id_db: #效率高 print(key,id_db[key])
set集合
set集合是一个无序且不重复的元素集合
1)创建集合
se1 = {11,22} print(type(se1)) se2 = set([22,33]) print(se2) 输出 <class ‘set‘> {33, 22}
2)操作集合
集合操作方法和前面的字符串、字典等类似
增加
#se1.add(44)
A存在B不存在
#se3 = se1.difference(se2)
A有B无 B有A无
#se4 = se1.symmetric_difference(se2)
更新se1
# se1.difference_update(se2)
移除(不存在不报错)
#se1.discard(11)
remove(不存在报错)
#se1.remove(11)
随机移除(返回移除的元素,这里pop里面不能加参数,而list里面pop可以有参数)
#ret = se1.pop()
交集
#se5 = se1.intersection(se2)
合并并集
#se6 = se1.union(se2)
update(接受一个可以被迭代的对象)
# lii = [11,2,3] # se8 = se1.update(lii) 不能以这种形式来更新 se1.update(lii) se7 = se1 print(se7)
小程序练习
题目:资产盘点结果中,找出CMDB项目中哪些要增加、哪些要被删除、哪些要更新。
这里以两个字典示范
old_dict = { "#1": 8, "#2": 4, "#4": 2, } new_dict = { "#1": 4, "#2": 4, "#3": 2, } set_old = set(old_dict) set_new = set(new_dict) set_del = set_old.difference(set_new) set_add = set_new.difference(set_old) set_update = set_old.intersection(set_new)
本文出自 “改变从每一天开始” 博客,请务必保留此出处http://lilongzi.blog.51cto.com/5519072/1860872
以上是关于自动化运维Python系列之基础数据类型的主要内容,如果未能解决你的问题,请参考以下文章