一、列表和元组
列表是我们最常用到的数据类型之一,在一个变量中定义多个变量,可以理解为数组 定义列表 1
2
3
4
5
6
| >>> name = ["liunx01","linux02","Unix",22]
>>> print(name)
['liunx01', 'linux02', 'Unix', 22]
>>> name[0]
'liunx01'
>>> name[-1]
|
步长 1
2
3
4
| >>> name
['liunx01', 'linux02', 'Unix', 29, 22]
>>> name[0::2]
['liunx01', 'Unix', 22]
|
切片,取多个元素
1
2
3
4
5
6
7
8
9
10
11
12
| >>> 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']
|
追加
1
2
3
| >>> name.append("CentOS")
>>> name
['liunx01', 'Unix', 22,'CentOS']
|
插入 1
2
3
| >>> name.insert(2,'RetHat')
>>> name
['liunx01', 'Unix','RetHat',22,'CentOS']
|
修改 1
2
3
| >>> name[1] = "CentOS"
>>> name
['liunx01', 'CentOS', 'RetHat', 22,'CentOS']
|
删除
1
2
3
4
5
6
7
| >>> name
['liunx01', 'CentOS', 'RetHat', 'Unix', 22]
>>> name.remove("RetHat") #指定元素删除
>>> del name[0:2] #切片删除
>>> name.pop() #删除最后一个value
>>> name
['Unix']
|
扩展
1
2
3
4
5
6
| >>> name
['liunx01', 'CentOS', 'RetHat', 'Unix', 22]
>>> b = [1,2,3]
>>> name.extend(b)
>>> name
['liunx01', 'CentOS', 'RetHat', 'Unix', 22, 1, 2, 3]
|
统计 1
2
3
| >>> num = name.count(22)
>>> print(num)
1
|
倒序、排序
1
2
3
4
5
6
7
8
| >>> 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']
|
找索引位置 1
2
3
4
5
| name = [22,"linux01","linux02","Unix",22]
for i in range(name.count(22)):
ele_index = name.index(22)
name[ele_index] = 999
print(name)
|
判断列表中是否存在一个元素 1
2
3
4
5
6
| >>> name
['liunx01', 'linux02', 'Unix', 29, 22]
>>> print(2 in name)
False
>>> print(22 in name)
True
|
拷贝 1
2
3
4
5
| >>> name
['liunx01', 'CentOS', 'RetHat', 'Unix', 22]
>>> name_copy = name.copy()
>>> name_copy
['liunx01', 'CentOS', 'RetHat', 'Unix', 22]
|
列表的拷贝可不止上面那么简单: 共享列表第二层内存地址 1
2
3
4
5
6
7
| 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]
|
元组 元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表 1
| names = ("alex","jack","eric")
|
它只有2个方法,一个是count,一个是index
字符串操作 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| 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","3!@#$%^")
print(s.translate(p))
H$llo Worl#!
b="ddefdsdff_哈哈"
print(b.isidentifier()) #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True
|
字典 由于列表中嵌套列表的做法不便于我们取值,所以就有了字典 字典的特性:key-value、dict是无序的、key值必须唯一 字典的定义 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| 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'}}
|
增加(或者覆盖) 1
2
3
4
5
6
7
8
9
10
| 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'}}
|
删除 1
2
| del id_db[101] #删除整个value里面的数据
id_db[101].pop("addr") #删除单个数据
|
获取get 1
2
| v = id_db.get(102)
print(v)
|
获取值或者key 1
2
3
4
| 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])
|
字典转列表 1
2
3
| print(id_db.items())
dict_items([(100, {'name': 'linux', 'addr': 'shanghai', 'age': 24}),
(101, {'name': 'nuix', 'addr': 'anhui', 'age': 23})])
|
判断包含 1
2
3
4
5
| id_db.has_key(185185) #only in 2.x
if 185185 in id_db:
print("yes")
else:
print("no")
|
setdefault 1
2
3
4
5
| 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 1
2
3
4
5
| 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 1
2
| print(id_db.popitem()) #随机删除一个key,不要用这个随机删除
(100, {'name': 'linux', 'age': 24, 'addr': 'shanghai'})
|
循环 1
2
3
4
5
6
7
| '''
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)创建集合 1
2
3
4
5
6
7
| se1 = {11,22}
print(type(se1))
se2 = set([22,33])
print(se2)
输出
<class 'set'>
{33, 22}
|
2)操作集合 集合操作方法和前面的字符串、字典等类似
增加
A存在B不存在 1
| #se3 = se1.difference(se2)
|
A有B无 B有A无 1
| #se4 = se1.symmetric_difference(se2)
|
更新se1 1
| # se1.difference_update(se2)
|
移除(不存在不报错)
remove(不存在报错)
随机移除(返回移除的元素,这里pop里面不能加参数,而list里面pop可以有参数)
交集 1
| #se5 = se1.intersection(se2)
|
合并并集
update(接受一个可以被迭代的对象)
1
2
3
4
5
| # lii = [11,2,3]
# se8 = se1.update(lii) 不能以这种形式来更新
se1.update(lii)
se7 = se1
print(se7)
|
小程序练习 题目:资产盘点结果中,找出CMDB项目中哪些要增加、哪些要被删除、哪些要更新。 这里以两个字典示范 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| 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)
|
|