Python--列表

1.列表的创建

数组:存储同一种数据类型的集合 scores = [1,2,3]
列表:可以存储任意数据类型的集合

#列表里:可以存储不同的数据类型

li = [1,1.2,True,‘hello’]
print(li)
print(type(li))

#列表里也可以嵌套列表(列表:本身也是一种数据类型)

li1 = [1,1.2,True,‘hello’,[1,2,3,4,5]]
print(li1)
print(type(li1))

2.列表的特性
service = [‘http’,‘ssh’,‘ftp’]

#索引

print(service[0])
print(service[-1])

#切片

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

#重复

print(service * 3)

#连接

service1 = [‘mysql’,‘firewalld’]
print(service + service1)

#成员操作符

print(‘firewalld’ in service)
print(‘firewalld’ in service1)

#for循环遍历

for se in service:
print(se)

#列表里嵌套列表

service2 = [[‘http’,80],[‘ssh’,22],[‘ftp’,21]]

#索引

print(service2[1][1])
print(service2[-1][1])

#切片

print(service2[:][1])
print(service2[:-1][0])
print(service2[0][:-1])

3.列表的增加
service = [‘http’,‘ssh’,‘ftp’]

#1.

print(service + [‘firewalld’])

#2.append:追加一个元素到列表中

service.append(‘firewalld’)
print(service)

#3.extend:拉伸 追加多个元素到列表中

service.extend([‘mysql’,‘firewalld’])
print(service)

#4.insert:在指定索引位置插入元素

service.insert(1,‘samba’)
print(service)

4.列表的删除
1.pop
##弹出最后一个元素

In [1]: service = [‘http’,‘ssh’,‘ftp’]
In [2]:
In [2]: service.pop()
Out[2]: ‘ftp’
In [3]: service
Out[3]: [‘http’, ‘ssh’]
In [4]: service.pop()
Out[4]: ‘ssh’
In [5]: service
Out[5]: [‘http’]
In [6]: service.pop()
Out[6]: ‘http’
In [7]: service
Out[7]: []
In [8]: service.pop()

2.remove
##删除指定元素

service = [‘http’,‘ssh’,‘ftp’]
a = service.remove(‘ssh’)
print(service)
print(a)

3.del
##关键字 从内存中删除

service = [‘http’,‘ssh’,‘ftp’]
print(service)
del service
print(service)

5.列表的修改
service = [‘http’,‘ssh’,‘ftp’]

#通过索引,重新赋值

service[0] = ‘mysql’
print(service)

#通过切片

print(service[:2])
service[:2] = [‘samba’,‘ldap’]
print(service)

6.列表的查看
service = [‘ftp’,‘http’,‘ssh’,‘ftp’]

#查看出现的次数

print(service.count(‘ftp’))

#查看指定元素的索引值(可以指定索引范围查看)

print(service.index(‘ssh’))
print(service.index(‘ftp’,0,3)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值