【Python学习笔记】列表及元组

一、列表定义

  • 被定义一对方括号[ ]中的数据, 不同的数据使用逗号隔开
  • 样子:[ 数据1, 数据2, 数据3 ]
  • 列表属于可变数据
data_01 = [ "nginx", "redis", "mysql", "tomcat" ]
print(type(data_01))
print(data_01)

<class 'list'>	#类型列表
['nginx', 'redis', 'mysql', 'tomcat']
data_01 = [ [ "Mysql", "MriaDB" ], [ "nginx", "httpd" ], [ "tomcat" ] ]
print(type(data_01))
print(data_01)

<class 'list'>
[['Mysql', 'MriaDB'], ['nginx', 'httpd'], ['tomcat']]

1.列表解析

data = [ i for i in range(10) ]
print(data)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
data = [ i ** 2 for i in range(1,11) ]
print(data)

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

二、列表常规操作符

1.判断成员关系innot in

>>> data_01 = [ "nginx", "httpd", "tomcat" ]
>>> "httpd" in data_01
True
>>> "httpd" not in data_01
False
data = ["nginx","httpd","tomcat","mysql"]
choise = input("请输入服务名:")
if choise in data:
    print("%s服务存在" % choise)
else:
    print("%s服务不存在" % choise)
    
请输入服务名:mariadb
mariadb服务不存在

2.获取列表中元素的个数

data = ["nginx","httpd","tomcat","mysql"]
print(len(data))
4

3.索引操作

data = ["mysql","redis","k8s","nginx"]
print(data[0])
mysql

data = ["mysql","redis","k8s","nginx"]
print(data[-1])
nginx

data_01 = [["nginx","1.20"],["mysql","5.6"]]
print(data_01[1])
print(data_01[1][0])
['mysql', '5.6']
mysql

修改数据

data = ["mysql","redis","k8s","nginx"]
data[0] = "mariadb"
print(data)
['mariadb', 'redis', 'k8s', 'nginx']

data_01 = [["nginx","1.20"],["mysql","5.6"]]
data_01[0][-1] = "latest"
print(data_01)
[['nginx', 'latest'], ['mysql', '5.6']]

三、列表对象操作方法

1.追加数据

data = ["nginx","httpd","tomcat","mysql"]
data.append("mariadb")
print(data)
['nginx', 'httpd', 'tomcat', 'mysql', 'mariadb']

2.插入数据

在指定位置插入数据

data = ["nginx","httpd","tomcat","mysql"]
data.insert(1, "apache")
print(data)
['nginx', 'apache', 'httpd', 'tomcat', 'mysql']
data = []
while True:
    choise_input = input("请输入你要插入的数据(输入q退出):")
    if choise_input == "quit" or choise_input == "q":
        break
    data.append(choise_input)
    print(data)
    
请输入你要插入的数据(输入q退出):nginx
['nginx']
请输入你要插入的数据(输入q退出):tomcat
['nginx', 'tomcat']
请输入你要插入的数据(输入q退出):httpd
['nginx', 'tomcat', 'httpd']

3.删除数据pop()

  • 删除最后一个数据
  • 返回被删除的数据
data = ["nginx","httpd","tomcat","mysql"]
new_data =data.pop()
print(new_data)
mysql

4.删除数据remove()

data = ["nginx","httpd","tomcat","mysql"]
data.remove("tomcat")
print(data)
['nginx', 'httpd', 'mysql']

四、遍历列表

app = ["nginx","redis","nfs","mysql"]
for i in app:
    print("yum install -y %s" % i)
    
yum install -y nginx
yum install -y redis
yum install -y nfs
yum install -y mysql

2种写法

image = [ ["mysql", "5.7"], ["nginx", "1.18"], ["wordpress", "latest"] ]
for i in image:
    print("docker pull %s:%s" % (i[0], i[1]))

docker pull mysql:5.7
docker pull nginx:1.18
docker pull wordpress:latest


image = [ ["mysql", "5.7"], ["nginx", "1.18"], ["wordpress", "latest"] ]
for i, j in image:
    print("docker pull %s:%s" % (i, j))

docker pull mysql:5.7
docker pull nginx:1.18
docker pull wordpress:latest

五、元组管理操作

1.元组的定义

  • 被定义在一对圆括号( )的数据, 不同的元素使用逗号隔开
  • 注意:元组是不可变的数据
data_01 = ()
data_02 = ( "nginx", "httpd", "IIS" )
data_03 = ( ("MySQL", "Oracle"), ("redis", "etcd"), ("docker", "podman") )
data_04 = ( ["linux", "unix"], ["macos", "windows"] )

print(type(data_01), type(data_02), type(data_03), type(data_04))

<class 'tuple'> <class 'tuple'> <class 'tuple'> <class 'tuple'>

2.元组的常用操作

len()innot in索引

  • 和列表一样的操作方法
  • 但是唯一区别是,内容不可修改
data_02 = ( "nginx", "httpd", "IIS" )
print(data_02[-1])
print(data_02[0])
IIS
nginx

3.遍历元组

data_01 = ( "nginx", "httpd", "tomcat", "IIS" )

for i in data_01:
    print("---> %s" % i)
    
---> nginx
---> httpd
---> tomcat
---> IIS
data = [("mysql","5.7"),("nginx","latest"),("redis","3.0.4")]
for a,b in data:
    print("docker run -itd %s:%s" %(a,b))
    
docker run -itd mysql:5.7
docker run -itd nginx:latest
docker run -itd redis:3.0.4