Python--列表

    xiaoxiao2025-03-04  51

    一.列表的基本定义 列表是最常用的Python数据类型。 列表的数据项不需要具有相同的类型,可以存储任意的数据类型 序列都可以进行的操作包括索引,切片,加,乘,检查成员等。 序列中的每个元素都分配一个索引,第一个索引是0,第二个索引是1,依此类推 二.列表的创建 创建列表时,列表中的元素以,分隔开来,列表以[ ]括起来 创建列表时也可使用嵌套列表,即一个新的列表由新元素与另外一个列表组成

    >>> list = [1,2,3.3,'westos',True] >>> list [1, 2, 3.3, 'westos', True] >>> list2 = [1,2,1.2,'westos',True,list] >>> list2 [1, 2, 1.2, 'westos', True, [1, 2, 3.3, 'westos', True]]

    三.列表的特性 1.索引 (1)正向索引

    >>> service = ['http','ssh','ftp'] >>> print(service[0]) ##从左到右索引第一个元素 http

    (2)反向索引

    >>> print(service[-1]) ##从右至左索引第一个元素 ftp

    2.切片

    >>> print(service[::-1]) ##将列表反转 ['ftp', 'ssh', 'http'] >>> print(service[1:]) ##除第一个元素以外的为所有元素 ['ssh', 'ftp'] >>> print(service[:-1]) ##除最后一个元素以外的所有元素 ['http', 'ssh']

    3.重复 list*n ##n:重复的次数

    >>> print(service*4) ##将列表重复输出4次 ['http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp', 'http', 'ssh', 'ftp']

    4.连接

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

    5.成员操作符 in ##判断元素是否属于该列表,存在为真,不存在为假 not in ##判断元素是否不属于该列表,不存在为真,存在为假

    >>> print('firewalld' in service) ## False >>> print('http' in service) True >>> print('ssh' not in service) False

    三.列表元素的增加与删除 1.增加 (1)append ##追加一个元素到列表

    >>> service = ['http','ftp','ssh'] >>> service.append('firewalld') >>> print(service) ['http', 'ftp', 'ssh', 'firewalld']

    (2)extend ##追加多个元素到列表

    >>> service.extend(['mysql','nfs']) >>> print(service) ['http', 'ftp', 'ssh', 'firewalld', 'mysql', 'nfs']

    2.删除 (1)pop() ##默认删除最后一个元素,删除的元素可被变量吸收

    >>> service = ['ftp','firewalld','ftp','ssh'] >>> service.pop() ##默认删除最后一个元素 'ssh' >>> service ['ftp', 'firewalld', 'ftp'] >>> a = service.pop() >>> a 'ftp' >>> service ['ftp', 'firewalld'] >>> service.pop(0) ##指定删除第一个元素 'ftp' >>> service ['firewalld'] >>> service.pop() 'firewalld' >>> service [] >>> service.pop() Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: pop from empty list

    (2)remove ##删除列表元素表,被删除的元素不可被变量吸收

    >>> service = ['http','ftp','ssh'] >>> a = service.remove('ftp') >>> print(a) None >>> print(service) ['http', 'ssh']

    (3)del(变量) ##直接从内容中删除

    >>> del service[0] >>> print(service) ['ssh']

    四.列表元素的查看、排序与修改 1.查看

    >>> service = ['ftp','http','http','ssh','ftp','ssh','ftp','ssh'] >>> print(service.count('ftp')) ##查看元素出现的次数 3 >>> print(service.index('ssh')) ##查看元素,并返回最小索引值 3 >>> print(service.index('ssh',4,7)) ##在指定范围内查看元素并返回最小索引值 5

    2.排序

    >>> service = ['http','ftp','ftp','ssh'] >>> service.sort(reverse=True) ##倒叙排列 >>> print(service) ['ssh', 'http', 'ftp', 'ftp'] >>> service.sort() ##正序排列 >>> print(service) ['ftp', 'ftp', 'http', 'ssh'] >>> phone = ['a','k','F','z'] >>> phone.sort(key=str.upper) ##忽略大小写排序 >>> phone ['a', 'F', 'k', 'z']

    3.随机打乱列表

    >>> import random # 加载模块 >>> a = list(range(10)) # 生成列表 >>> a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 列表原来的内容 >>> random.shuffle(a) # 打乱列表元素顺序 >>> a [2, 0, 7, 3, 6, 4, 1, 9, 5, 8] # 打乱之后的列表 >>> b = ['c','d','e','f','g'] # 列表原来的内容 >>> random.shuffle(b) >>> b ['c', 'g', 'f', 'e', 'd'] # 打乱之后的列表

    4.修改 (1)通过索引值,进行赋值

    >>> service = ['ftp','http','ftp','ssh','mysql'] >>> service[0] = 'samba' ##将索引0的值做更改 >>> service ['samba', 'http', 'ftp', 'ssh', 'mysql']

    (2)通过切片赋值

    >>> service[:2]=['mysql','nginx','nfs'] ##前两个元素做更改 >>> service ['mysql', 'nginx', 'nfs', 'ftp', 'ssh', 'mysql']

    五.练习 1.假定有下面这样的列表: names = [‘fentiao’, ‘fendai’, ‘fensi’, ‘apple’] 输出结果为:'I have fentiao, fendai, fensi and apple.'

    name = ['fentiao','fendai','fensi','apple'] print('I have %s,%s,%s and %s' %(name[0],name[1],name[2],name[3]))

    2.题目:输入某年某月某日(yyyy-MM-dd),判断这一天是这一年的第几天?

    Time = input('请输入日期:yyy-MM-dd') date = Time.split('-') year = int(date[0]) mouth = int(date[1]) day = int(date[2]) num = 0 arr1 = [0,31,29,31,30,31,30,31,31,30,31,30,31] arr2 = [0,31,28,31,30,31,30,31,31,30,31,30,31] if ((year%4 == 0 or year0 != 0) and (year@0 == 0)): arr1 = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] for i in range(1,len(arr1)): if mouth > i: num +=arr1[i] else: num += day break print('第%d天' %(num+day)) else: for i in range(1,len(arr2)): if mouth > i: num +=arr1[i] else: num += day print('第%d天' %(num + day))

    3.题目:输入三个整数x,y,z,请把这三个数由小到大输出

    num = input('请输入三个数字:x y z') NUM = num.split(' ') list = [] for i in NUM: list.append(int(i)) list.sort() for k in list: print('%d\t' %(k) ,end='')
    最新回复(0)