Python——列表及其特性

列表

列表:类似与C中的数组

数组

存储同一数据类型的集合

列表

可以存储任意数据类型的集合
列表的表示:

In [1]: name = ['tom','alice','coco']                                           

In [2]: name                                                                    
Out[2]: ['tom', 'alice', 'coco']

In [3]: type(name)                                                              
Out[3]: list

在这里插入图片描述

创建列表

  • 创建列表
    列表中,可以存储不同的数据类型
#列表中,可以存储不同的数据类型
li = [1,1.5,'hello',True]
print(li)
print(type(li))

在这里插入图片描述

  • 列表里也可以嵌套列表(列表也是一种数据类型)
#列表里也可以嵌套列表(列表也是一种数据类型)
list = [1,1.2,'hello',True,[1,2,3,4]]
print(list)
print(type(list))

在这里插入图片描述

列表的特性

索引
service = ['http','nfs','ftp']
#索引
print(service[0])
print(service[1])
print(service[-1])    ##列表中最后一个元素

在这里插入图片描述

切片
service = ['http','nfs','ftp']
#切片
print(service[1:])      #第一个以后
print(service[:-1])     #最后一个之前
print(service[::-1])      #倒序

在这里插入图片描述

重复
#重复
service = ['http','nfs','ftp']
print(service*3)

在这里插入图片描述

连接
#连接
service = ['http','ftp','ssh']
service1 = ['nfs','samba']
print(service + service1)

在这里插入图片描述

成员操作符
#成员操作符
service = ['http','ssh','nfs']
print('nfs' in service)
print('https' in service)

在这里插入图片描述

迭代

迭代 就是是否可以用for循环去遍历,操作

#for循环遍历
service = ['http','ssh','nfs']
for i in service:
    print(i)

在这里插入图片描述

列表中嵌套列表

列表中嵌套列表
service = [['http','80'],['ssh','22'],['ftp','21']]
print(service)
print(type(service))

在这里插入图片描述

索引
#索引
service = [['http','80'],['ssh','22'],['ftp','21']]
print(service[0][1])     #第一行第二列,索引值默认从0开始
print(service[-1][-1])    #最后一行第二列

在这里插入图片描述

切片
#切片
service = [['http','80'],['ssh','22'],['ftp','21']]
print(service[:][1])     打印所有行的第二列
print(service[:-1][0])    除最后一列外,打印第一列

在这里插入图片描述

列表练习

输入月份,判断这个月属于哪个季节

代码如下:

month = int(input("请输入月份: "))
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("请您输入正确的值...")

执行结果:
在这里插入图片描述

有下面一个列表,请输出为下面的格式
"""
names = ['fentiao','fendai','fensi','fenpi']
输出: I have fentiao,fendai,fensi and fenpi
"""

代码如下:

names = ['fentiao','fendai','fensi','fenpi']
print('I have ' + ','.join(names[:-1]) + ' and ' + names[-1])

执行结果:
在这里插入图片描述

按格式输入某年某月某天,判断这一天是这一年的第几天
"""
年月日:YYYY-MM-DD
"""

代码如下:

time = input("请按照这个格式输入日期,年月日:YYYY-MM-DD:")
date = time.split('-')
year = int(date[0])
month = int(date[1])
day = int(date[2])

list = [31,28,31,30,31,30,31,31,30,31,30,31]   #定义一个普通列表
num = 0

if ((year % 4 == 0) and (year % 100 != 0) or (year % 400 == 0)):   #是闰年
    list[1] = 29   #修改2月为31天
for i in range(12):  #从0开始
    if month > i + 1:
        num += list[i]
    else:
        num += day 
        break
print('这一天是%d年的第%d天' %(year,num))

执行结果:
在这里插入图片描述
在这里插入图片描述

列表的增加

方法一
service = ['http','ssh','nfs']
print(service + ['samba'])

在这里插入图片描述

方法二:append:追加,追加一个元素到列表中
#append:追加,追加一个元素到列表中
service = ['http','ssh','nfs']
service.append('firewalld')
print(service)

在这里插入图片描述

方法三:extend:拉伸 追加多个元素到列表中
#extend:拉伸 追加多个元素到列表中
service = ['http','ssh','nfs']
service.extend(['mysql','ntp'])
print(service)

在这里插入图片描述

方法四:insert:在指定索引位置插入元素
#insert:在指定索引位置插入元素
service = ['http','ssh','nfs']
service.insert(1,'firewalld')
print(service)

在这里插入图片描述

列表的删除

方法一:pop弹出,删除 默认弹出最后一个元素
In [1]: service = ['http','ssh','ftp']                                          

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

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

In [4]: a = service.pop()                                                       

In [5]: a                                                                       
Out[5]: 'ssh'

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

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

In [8]: service                                                                 
Out[8]: []

在这里插入图片描述

方法二:remove 删除指定的元素
In [1]: service = ['http','ssh','ftp']                                          

In [2]: a = service.remove('ftp')                                               

In [3]: a                                                                       

In [4]: print(service)                                                          
['http', 'ssh']

In [5]: print(a)                                                                
None

在这里插入图片描述

方法三:del 直接从内存中删除列表
In [1]: service = ['http','ssh','ftp']                                          

In [2]: del service                                                             

In [3]: print(service)                                                          
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-eb3457d971bb> in <module>
----> 1 print(service)

NameError: name 'service' is not defined

在这里插入图片描述

列表的修改

通过索引,重新赋值
service = ['http','ssh','ftp']
#通过索引,重新赋值
service[0] = 'mysql'
print(service)

在这里插入图片描述

通过切片
#通过切片
service = ['http','ssh','ftp']
print(service[:2])
service[:2] = ['samba','nfs']
print(service)

在这里插入图片描述

列表的查看

查看出现的次数
service = ['ssh','http','ssh','ftp']
#查看出现的次数
print(service.count('ssh'))

在这里插入图片描述

查看指定元素的索引值(可以指定索引范围查看)
service = ['ssh','http','ssh','ftp']
#查看指定元素的索引值
print(service.index('ssh'))
print(service.index('ssh',1,3))

在这里插入图片描述

排序

默认是根据ASCII码的值排序

对字符串排序不区分大小写,虽然指定了但还是会按照默认的排序

names = ['alice','Bob','coco','Harry']
print(names.sort())
print(names)

在这里插入图片描述

names = ['alice','Bob','coco','Harry']
names.sort(key=str.lower)
print(names)

在这里插入图片描述

将列表打乱
list = list(range(10))
print(list)
#将列表打乱
import random
random.shuffle(list)
print(list)

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值