博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中的列表
阅读量:7094 次
发布时间:2019-06-28

本文共 2478 字,大约阅读时间需要 8 分钟。

序列是Python中最基本的数据结构。序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推。

1.列表
数组:存储同一种数据类型的集合 scores = [12,23,45]
列表(打了激素的数组):可以存储任意数据类型

list = [1,1.2,True,'westos']

print(list,type(list))
与字符串的索引一样,列表索引从0开始。列表可以进行截取、组合等。

Python中的列表

#列表里面也可以嵌套列表
list2 = [1,2,3,4,[1,1.2,True,'westos']]
print(list2,type(list2))
Python中的列表
Python中的列表

2.列表的特性

#索引

#正向索引
service = ['http','ftp','ssh']
print(service[0])
#反向索引
print(service[-1])
Python中的列表
#切片
print(service[::-1]) # 列表元素序列反转
print(service[1:]) #列表中除了第一个元素之外的元素
print(service[:-1]) # 列表中除了最后一个元素之外的元素
Python中的列表
#重复
print(service * 3) #重复三次
['http', 'ftp', 'ssh', 'http', 'ftp', 'ssh', 'http', 'ftp', 'ssh'] 生成的新列表
#连接
service1 = ['mysql','firewalld']
print(service + service1)
Python中的列表
#成员操作符
in #判断元素是否属于该列表 属于为真 不属于为假
not in #判断元素是否不属于该列表 属于为真 不属于为假
print('firewalld' in service) 判断firewalld是否是列表中的元素
print('ftp' in service)
print('mysql' not in service)

假定有下面这样的列表:

names = ['fentiao', 'fendai', 'fensi', 'apple']
输出结果为:'I have fentiao, fendai, fensi and apple.'

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

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

1.cal = input('请输入日期 yyyy-MM-dd:')
date = cal.split('-') #拆分日期
year = int(date[0])
month = int(date[1])
day = int(date[2])
arr = [0,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)):
arr[2] = 29
for i in range(1,len(arr)):
if month > i:
num += arr[i]
else:
num += day
break
print('天数:',num)

2.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%400 == 0 or (year%4 == 0 and year%100 != 0)) :
for i in list2[:mon] :
k+=i
print('第%d 天' %(k+day))
else :
for i in list1[:mon] :
k+=i
print('第%d 天' %(k+day))
Python中的列表

列表元素的增加

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

#append():追加一个元素到列表

service.append('firewalld')
print(service)

#extend():拉伸 追加多个元素到列表中

service.extend(['mysql','nfs'])
print(service)

#insert() 在指定索引处插入元素

service.insert(1,'https')
print(service)

#remove:删除列表元素

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

#从内存中删除一个数据

del service[1]
print(service)
service = ['ftp','http','http','ssh','ftp','ssh','ftp','ssh']

#查看元素在列表中出现的次数

print(service.count('ftp'))

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

print(service.index('ssh'))
print(service.index('ssh',4,7))
import random

列表元素的排序 # 默认是按照Ascii码进行排序的

li = list(range(100))
print(li)
random.shuffle(li)
print(li)

Python包含以下方法:

Python中的列表

转载于:https://blog.51cto.com/12893781/2401086

你可能感兴趣的文章
Code Force 429B Working out【递推dp】
查看>>
解决win7 64位操作系统下安装PL/SQL后连接报错问题: make sure you have the 32 bits oracle client installed...
查看>>
nonatomic,assign,copy,retain的区别
查看>>
nginx,linux压力测试工具webbench
查看>>
进程学习第一课--基本操作
查看>>
java1.8--OptionalInt,OptionalDouble,OptionalLong类
查看>>
Wireshark网络分析实战笔记(三)基本信息统计工具的使用方法
查看>>
mysql 经常使用命令整理总结
查看>>
【JAVA笔记——器】Spring面向切面编程 Aspect Oriented Programming with Spring
查看>>
Oracle监听静态注册和动态注册
查看>>
RHEL7-Samba共享测试
查看>>
ubuntu下 php 笔记
查看>>
js传输txt文档内容
查看>>
ThreadLocal与线程池使用的问题
查看>>
Linux时间子系统(十二) periodic tick
查看>>
死锁原因及解决、避免办法
查看>>
TF-IDF与余弦相似性的应用
查看>>
HTML盒子尺寸的计算
查看>>
java开发常用技术
查看>>
SQLServer中对Xml字段的操作
查看>>