一、列表定义及操作
数组: 存储同一种数据类型的集和。scores=[12,95.5] 列表(打了激素的数组): 可以存储任意数据类型的集和。
list = []
list = [['172','25'],['25','254'],['254','100']]
创建列表
li = [1]
print(li,type(li))
列表连接、重复
print([1,2]+[3,4])
print([1,2]*3)
存在判断
print(1 in ['1','True',2])
print(1 in ['a','False',2])
索引
la = [1,2,3,[1,2]]
print(la[0])
print(la[-1])
print(la[-1][0])
print(la[3][-1])
切片
li = ['172','25','254','51']
print(li[:2])
print(li[2:])
print(li[::-1])
拼接
print("-".join(li[::-1]))
二、列表内置方法
添加
append:
li = [1,2,3]
li.append(4)
print(li)
insert
li = [1,2,3]
li.insert(0,'cat')
li.insert(1,'westos')
print(li)
extend
li = [1,2,3]
li.extend([4,5,6])
print(li)
修改
li = [1,2,3]
li[0] = 'jia'
li[-1] = 'zhao'
print(li)
当添加元素大于列表长度时,效果等同于添加
li = [1,2,3]
li[:3] = ['jia','zhao','bo',1,1,1,1,1]
print(li)
查看
li = [1,2,3,1,2,3,3,2,]
print(li.count(2))
print(li.index(3))
删除
将pop存储的结果存放在变量中
li = [1,2,3]
delete_num = li.pop(-1)
print(li)
print("is",delete_num)
根据value值删除
li = [1,2,3]
li.remove(3)
print(li)
全部清空
li = [1,2,3]
li.clear()
print(li)
其他
翻转
li = [1,2,3,4]
li.reverse()
print(li)
排序
li = [1,2,3,4]
li.sort()
print(li)
li.sort(reverse=True)
拷贝
li = ['jia','zhao','bo']
licopy = li.copy()
print(id(li),id(licopy))
print(li,licopy)
三、元组定义与操作
- 特性: 连接&重复操作符、成员操作符、索引&切片
注意: 元组是不可变数据类型,不能对元组的值任意更改; 创建 注:单个值元组的创建要用 tuple = (fentiao,)
t1 = ()
print(t1,type(t1))
t2 = (1,)
print(t2,type(t2))
t3 = (1,1.1,True,[1,2,3])
print(t3,type(t3))
简单用法基本等同于上述列表操作,不再赘述
命名元组
t = ('shaanxi','xian','westos')
print(t[0],t[1],t[2])
from collections import namedtuple
User = namedtuple('User',('province','area','company'))
user1 = User('shaanxi','xian','jiren')
print(user1)
print(user1.area)
print(user1.company)
print(user1.province)
is和==区别
- Python中对象的三个基本要素,分别是:id(身份标识)、type(数据类型)和value(值)。
- s和==都是对对象进行比较判断作用的,但对对象比较判断的内容并不相同。
- ==用来比较判断两个对象的value(值)是否相等;(type和value)
- is也被叫做同一性运算符, 会判断id是否相同;(id, type 和value)
li = ['jia','zhao','bo']
licopy = li.copy()
print(id(li),id(licopy))
print(li,licopy)
print(li==licopy)
print(li is licopy)
练习
构建虚拟主机建立系统,实现虚拟主机的增删改查 思路:创建命名元组,由于元组不可修改,还需将元组存入列表。
from collections import namedtuple
menu = """
vm menagent system
1) add vm host
2) search vm host
3) remove vm host
4) list vm host
5) exit system
please choose:
"""
hosts = []
Host = namedtuple('Host', ('ip', 'hostname', 'idc'))
while True:
choice = input(menu)
if choice == '1':
print('add vm host'.center(50,'*'))
ip = input('ip:')
hostname = input('hostname:')
idc = input('idc(eg:huawei,ali,tencent):')
host1 = Host(ip, hostname, idc)
hosts.append(host1)
print(f"{idc}add success.IP is {ip}")
elif choice == '2':
print('search vm host'.center(50,'*'))
elif choice == '3':
print('remove vm host'.center(50,'*'))
elif choice == '4':
print('list vm host'.center(50,'*'))
print('ip\thostname\tidc')
count = 0
for host in hosts:
count += 1
print(f"{host.ip}\t{host.hostname}\t{host.idc}")
print('the vm hosts count is',count)
elif choice == '5':
print('it is exiting')
exit()
else:
print('please input true choice')
四、浅拷贝和深拷贝
import copy
a1 = [1,2,3,[1,2]]
a2 = a1.copy()
a1[-1].append(4)
print(id(a1),id(a2))
print(id(a1[-1]),id(a2[-1]))
print(id(a1[-2]),id(a2[-2]))
print(a1)
print(a2)
a1 = [1,2,3,[1,2]]
a2 = copy.deepcopy(a1)
a1[-1].append(4)
print(id(a1),id(a2))
print(id(a1[-1]),id(a2[-1]))
print(id(a1[-2]),id(a2[-2]))
print(a1)
print(a2)
|