python的列表详解

41 篇文章 5 订阅

一. 列表的创建

  • 数组:存储同一种数据类型的集合 scores = [1,2,3]
  • 列表:可以存储任意数据类型的集合
a = [1,2.2,True,'hello']
print(a,type(a))
输出结果:
[1, 2.2, True, 'hello'] <class 'list'>
In [1]: name1 = 'tom'                                                   

In [2]: name2 = 'Tony'                                                  

In [3]: name3 = 'coco'                                                  

In [4]: name1                                                           
Out[4]: 'tom'

In [5]: name2                                                           
Out[5]: 'Tony'

In [6]: name3                                                           
Out[6]: 'coco'

In [7]:                                                                 

In [7]: name = ['tom','Tony','coco']                                    

In [8]:                                                                 

In [8]: name                                                            
Out[8]: ['tom', 'Tony', 'coco']

In [9]: type(name)                                                      
Out[9]: 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']
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']

练习:

  • 根据用于指定月份,打印该月份所属的季节。
  • 提示: 3,4,5 春季 6,7,8 夏季 9,10,11 秋季 12, 1, 2 冬季
month = int(input('Month:'))
if month in [3,4,5]:
    print('春季')
elif month in [6,7,8]:
    print('夏季')
elif month in [9,10,11]:
    print('秋季')
elif month in [12,1,2]:
    print('冬季')
else:
    print('输入不合法')

2.假设有下面这样的列表:
names = ['fentiao','fendai','fensi','apple']
输出结果为:'I have fentiao, fendai, fensi and apple.'

names = ['fentiao','fendai','fensi','apple']

print('I have ' + ','.join(names[:-1]) + ' and ' + names[-1])

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

  • 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']

2. 列表的删除

  • pop() 弹出最后一个元素
  • pop(1) 弹出索引值为1的元素
  • remove(‘元素’) 移除元素
  • clear() 清空列表里所有元素
  • del(python关键字) 从内存中删除列表

(1)pop

In [12]: service = ['http','ssh','ftp']                                 

In [13]:                                                                

In [13]: service.pop()                                                  
Out[13]: 'ftp'

In [14]: service                                                        
Out[14]: ['http', 'ssh']

In [15]: service.pop()                                                  
Out[15]: 'ssh'

In [16]: service                                                        
Out[16]: ['http']

In [17]: service.pop()                                                  
Out[17]: 'http'

In [18]: service                                                        
Out[18]: []

In [19]: service.pop()                                                  
------------------------------------------------------------------------
IndexError                             Traceback (most recent call last)
<ipython-input-19-d8278ea86206> in <module>
----> 1 service.pop()

IndexError: pop from empty list

(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)
del service
print(service)
结果:
['http', 'ssh', 'ftp']
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)
del service[1]
print(service)
结果:
['http', 'ssh', 'ftp']
['http', 'ftp']

3. 列表的修改

(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)

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

4. 列表的查看

(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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值