创建一个列表,只要使用逗号作为分隔符号,将不同的数据项隔开并使用方括号括起来
列表里面也可以嵌套列表
数组:存储同一种数据类型的集合 scores = [12,23,45] 列表(打了激素的数组):可以存储任意数据类型
list = [1,1.2,True,'westos'] print(list,type(list)) 包含整数,浮点型,bool型,字符串 >>> list= [1,1.2,True,'westos'] >>> print(list,type(list)) ([1, 1.2, True, 'westos'], <type 'list'>) >>>正向索引 print(service[0])
>>> service = ['ftp','http','https'] >>> print(service[0]) 从左到右引为0的元素 ftp >>>反向索引 print(service[-1])
>>> service = ['ftp','http','https'] >>> print(service[-1]) 从右到左第一个元素 https >>>切片 print(service[::-1]) print(service[1:]) print(service[:-1])
>>> service = ['ftp','http','https'] >>> print(service[::-1]) 元素列表反转 ['https', 'http', 'ftp'] >>> print(service[1:]) 查看除去第一个元素的所有元素 ['http', 'https'] >>> print(service[:-1]) 查看除去最后一个元素的所有元素 ['ftp', 'http'] >>>将两个列表合并成一个列表 :格式 list1 + list2
>>> name = ['lala','toto','haha','dodo'] #第一个列表 >>> name1 = ['qoqo','wowo'] #第二个列表 >>> name + name1 #合并列表 ['lala', 'toto', 'haha', 'dodo', 'qoqo', 'wowo'] #生成一个新的列表in #判断元素是否属于该列表 属于为真 不属于为假 not in #判断元素是否不属于该列表 属于为真 不属于为假
>>> service = ['ftp','http','https'] >>> print ('firewall' in service) False >>> print ('http' in service) True >>> print ('mysql' not in service) True1 假定有下面这样的列表:
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] )2 题目:输入某年某月某日(yyyy-MM-dd),判断这一天是这一年的第几天?
date=input('请输入日期: ') date1=date.split('-') year=int(date1[0]) mon=int(date1[1]) day=int(date1[2]) k=0 list1=[0,31,28,31,30,31,30,31,31,30,31,30,31] list2=[0,31,29,31,30,31,30,31,31,30,31,30,31] if (year@0 == 0 or (year%4 == 0 and year0 != 0)) : for i in list2[:mon] : k+=i print('第%d 天' %(k+day)) else : for i in list1[:mon] : k+=i print('第%d 天' %(k+day))1 append(元素)追加一个元素到列表,默认添加到末尾
>>> name = ['lala','toto','haha','dodo'] #定义列表 >>> name.append('tete') #追加一个元素到列表 >>> name ['lala', 'toto', 'haha', 'dodo', 'tete'] # 显示追加之后的列表注意:使用pop()删除的元素可以由另外一个变量进行接收
>>> s ['beijing', 'shanxi', 'hebei', 'henan', 'tianjin'] # 列表中的所有元素 >>> s.pop() # 默认删除列表最后一个元素 'tianjin' >>> s ['beijing', 'shanxi', 'hebei', 'henan'] # 删除之后的列表 >>> s.pop(0) # 指定索引删除特定的元素 'beijing' >>> s ['shanxi', 'hebei', 'henan'] >>> a = s.pop() # 将删除的元素由变量a接受 >>> a # a的值为刚刚被删除的元素 'henan' >>>默认是按照Ascii码进行排序的
sort() #正序 sort(reverse=True) #倒序 sort(key=str.upper) # 忽略大小写
>>> s = ['a','f','A','H'] # 定义字符串 >>> s.sort() # 按照ascii 码进行排序 >>> s ['A', 'H', 'a', 'f'] # 排序结果 >>> s.sort(reverse=True) # 倒叙=倒序 >>> s ['f', 'a', 'H', 'A'] # 排序结果 >>> s.sort(key=str.upper) # 忽略大小写进行排序 >>> s ['a', 'A', 'f', 'H'] #排序结果 >>>import random # 安装模块
random.shuffle(列表) # 打乱列表中元素的顺序
>>> 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'] # 打乱之后的列表题目:输入三个整数x,y,z,请把这三个数由小到大输出:
num = input('请按照格式X,Y,Z 依次输入数字 : ') num1 = num.split(',') a =int(num1[0]) b=int(num1[1]) c=int(num1[-1]) li=[a,b,c] li.sort() print(li)