Python——列表简介(索引,切片,重复,连接,成员操作符,迭代....)

一. 列表的创建

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

a = [1,2.2,True,'hello']
print(a,type(a))
输出结果:
[1, 2.2, True, 'hello'] <class 'list'>

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

li = [1,2,3,False,'python',[1,2,3,4,5]]
print(li,type(li))
输出结果:
[1, 2, 3, False, 'python', [1, 2, 3, 4, 5]] <class 'list'>

随机输出一个乱序的列表

import random
li = list(range(10))
#快速生成列表
random.shuffle(li)
print(li)

结果:
[6,4,5,3,9,8,1,0,7,2]  ##乱序

二. 列表的特性

索引

service = ['http','ssh','ftp']
print(service[0]) ##第一个
print(service[-1]) ##最后一个

输出结果:
http
ftp

切片

service = ['http','ssh','ftp']
print(service[1:]) ##除了第一个不显示剩下的都显示
print(service[:-1]) #除了最后一个不显示剩下的都显示
print(service[::-1]) ##倒过来显示

输出结果:
['ssh', 'ftp']
['http', 'ssh']
['ftp', 'ssh', 'http']

重复

service = ['http','ssh','ftp']
print(service * 3)

输出结果:
['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']

连接

service = ['http','ssh','ftp']
service1 = ['mysql','firewalld']
print(service + service1)

输出结果:
['http', 'ssh', 'ftp', 'mysql', 'firewalld']

成员操作符

service = ['http','ssh','ftp']
service1 = ['mysql','firewalld']
print('firewalld' in service)
print('firewalld' in service1)

输出结果:
False
True

for循环 (迭代)

service = ['http','ssh','ftp']
for se in service:
    print(se)

输出结果:
http
ssh
ftp

列表里嵌套列表

service2 = [['http',80],['ssh',22],['ftp',21]]

#1.索引
print(service2[1][1])
print(service2[-1][1])

#2.切片
print(service2[:][1])
print(service2[:-1][0])
print(service2[::-1][0])
print(service2[0][:-1])

输出结果:
22
21
['ssh', 22]
['http', 80]
['ftp', 21]
['http']

三、列表的增加,删除,修改,查看

1.列表的增加

  • 用+[’ ’]的方式

  • append(‘元素’)追加一个元素

  • extend([‘元素1’,'元素2 '])追加多个元素

  • insert(索引值,‘元素’)

      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)
              
              结果:
              ['http', 'ssh', 'ftp', 'firewalld']
              ['http', 'ssh', 'ftp', 'firewalld']
              ['http', 'ssh', 'ftp', 'firewalld', 'mysql', 'firewalld']
              ['http', 'samba', 'ssh', 'ftp', 'firewalld', 'mysql', 'firewalld']
    
  1. 列表的删除
  • pop() 弹出最后一个元素
  • pop(1) 弹出索引值为1的元素
  • remove(‘元素’) 移除元素
  • clear() 清空列表里所有元素
  • del(python关键字) 从内存中删除列表

(1)pop

service = ['http','ssh','ftp']                                 
service.pop() ##弹出了最后一个元素ftp                                                  
service ##查看剩余元素                                                       
['http', 'ssh'] ##不存在ftp元素
 service.pop()                                                  
service                                                        
['http']

(2)remove

#remove:删除指定元素
service = ['http', 'ssh', 'ftp']
a = service.remove('ssh')
print(service)
print(a)

结果:
['http', 'ftp']
None

(3)clear

service = ['http', 'ssh', 'ftp']

service.clear()	#清空列表里面的所有元素
print(service)

结果:
[]

(4)del

service = ['http', 'ssh', 'ftp']
print(service)
['http', 'ssh', 'ftp']
del service
print(service)
Traceback (most recent call last):
  File "/home/kiosk/PycharmProjects/westos/hui.py", line 12, in <module>
    print(service)
NameError: name 'service' is not defined

service = ['http', 'ssh', 'ftp']
print(service)
['http', 'ssh', 'ftp']
del service[1]
print(service)
['http', 'ftp']
  1. 列表的修改

(1)通过索引,重新赋值

service = ['http','ssh','ftp']
service[0] = 'mysql'
print(service)

结果:
['mysql', 'ssh', 'ftp']

(2)通过切片

service = ['http','ssh','ftp']
print(service[:2])
service[:2] = ['samba','ldap']
print(service)

结果:
['http', 'ssh']
['samba', 'ldap', 'ftp']
  1. 列表的查看

(1)查看出现的次数

service = ['ftp','http','ssh','ftp']
print(service.count('ftp'))
结果:
2

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

service = ['ftp','http','ssh','ftp']
print(service.index('ssh'))
print(service.index('ftp',0,3))列表的查看
结果:
2
0

四、列表中的排序

ASCII顺序查看:

service = ['ftp','http','ssh','ftp']
#排序查看 按照ASCII顺序
service.sort()
print(service)

结果:
['ftp', 'ftp', 'http', 'ssh']

逆序排序

service = ['ftp','http','ssh','ftp']
service.sort(reverse=True)
print(service)

结果:
['ssh', 'http', 'ftp', 'ftp']

 In [24]: names = ['alice','bob','harry','Borry']                        
 In [25]: names.sort()                                                   
 In [26]: names                                                          
Out[26]: ['Borry', 'alice', 'bob', 'harry']
In [27]: names.sort(key=str.lower)                                      
In [28]: names                                                          
Out[28]: ['alice', 'bob', 'Borry', 'harry']
In [29]: names.sort(key=str.upper)                                      
In [30]: names                                                          
Out[30]: ['alice', 'bob', 'Borry', 'harry']

打乱列表顺序

import random
li = list(range(10))
print(li)

#将原有的列表顺序打乱
random.shuffle(li)
print(li)

结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[8, 2, 5, 1, 9, 7, 3, 6, 0, 4]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值