Python中列表知识

列表- - -list

name = ['Tom', 'Lily', 'Coco']

列表中可以存储不同的数据类型,列表中可以进行嵌套

Lst = [1, 2, 'string', True, [1, 2, 3]]

列表的特性

Service = ['http', 'ftp', 'samba']

索引

Service[0]

切片

print(Service[1:])

重复

print(Service * 3)

连接

Service1 = ['nfs', 'iptables']
Service1 = Service + Service1
print(Service1)

成员操作符

print('nfs' in Service)

遍历

for i in Service:
    print(i)

列表中的嵌套

Service = [['http', 80], ['ssh', 22], ['ftp', 21]]
print(Service[1][1])

切片

print(Service[1][::-1])
print(Service[:][::-1])

增删改查

增加

Service = ['http', 'ssh']
Service = Service + ['nfs']

append 增加

Service.append('mysql')

extend 拉伸

Service.extend(['firewall', 'iptables'])

insert 插入

Service.insert(1, 'ftp')
print(Service)

删除

pop 默认最后一个,可加索引

Service = ['http', 'nfs', 'ssh']
Service.pop()
Service.pop(0)
print(Service)

remove 删除指定元素

Service = ['http', 'nfs', 'ssh']
Service.remove('http')
print(Service)
Service.remove('nfs')
print(Service)

del 从内存中直接删除

Service = ['http', 'nfs', 'ssh']
del Service

修改 索引/切片方式

Service = ['http', 'nfs', 'ssh']
Service[0] = 'mysql'
Service[1:2] = ['php', 'dhcp']
print(Service)

查询

Service = ['http', 'ssh', 'nfs']

出现次数

Service.count('ssh')

查询索引

print(Service.index('ssh'))
print(Service.index('ssh', 1, 2))

排序

Service = ['http', 'ssh', 'nfs']
Service.sort()
Service.sort(key=str.lower)
print(Service)

乱序

import random
Lst = list(range(10))
random.shuffle(Lst)
print(Lst)

序号

Service = ['http', 'ssh', 'nfs']
print(Service[1:2])
print(Service[:2])
print(Service[0:2])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值