Python
中的一些经常使用的数据类型的基础知识Number
)和字符串的一些函数和模块的使用Python
中的一序列(列表/元组/字典)先回顾下上一篇Python数据类型详解01文章中介绍的列表的基础知识html
# List 列表
list1 = [12, 34, 3.14, 5.3, 'titan']
list2 = [10, 'jun']
# 1.完整列表
print(list1)
# 2.列表第一个元素
print(list1[0])
# 3.获取第2-3个元素
print(list1[1:2])
# 4.获取第三个到最后的全部元素
print(list1[2:])
# 5.获取最后一个元素
print(list1[-1])
# 6.获取倒数第二个元素
print(list1[-2])
# 7.获取最后三个元素
print(list1[-3:-1])
# 8.合并列表
print(list1 + list2)
# 9.重复操做两次
print(list2 * 2)
复制代码
对列表的数据项进行修改或更新,你也可使用append()方法来添加列表项python
list1 = [] ## 空列表
list1.append('Google') ## 使用 append() 添加元素
list1.append('Baidu')
print(list1)
//输出:
['Google', 'Baidu']
复制代码
使用 del
语句来删除列表的元素数组
del list1[1]
//输出:
['Google']
复制代码
列表对 + 和 星号 的操做符与字符串类似。+ 号用于组合列表,星号 号用于重复列表app
# 1. 脚本操做符
list1 = [1, 2, 3]
# 元素个数
print(len(list1))
# 重复
list2 = [2] * 3
print(list2)
# 是否包含某元素
if (3 in list1):
print('3在列表内')
else:
print('3不在列表内')
# 遍历列表
for x in list1 :
print(x)
# 输出结果:
3
[2, 2, 2]
3在列表内
1
2
3
复制代码
下面将会列出在列表中经常使用的函数和方法函数
函数表达式 | 输出结果 | 描述 |
---|---|---|
len(list1) |
3 | 列表元素个数 |
max([1, 2, 's']) |
s | 返回列表元素的最大值 |
min([1, 2, 's']) |
1 | 返回列表元素的最小值 |
list(('q', 1) |
['q', 1] |
将元组转换为列表 |
list1.append(2) |
[1, 2, 3, 2] | 在列表末尾添加新的对象 |
list1.count(2) |
2 | 统计某个元素在列表中出现的次数 |
list1.index(3) |
2 | 从列表中找出某个值第一个匹配项的索引位置 |
list1.insert(1, 'jun') |
[1, 'jun', 2, 3, 2] | 将对象插入列表的指定位置 |
list1.remove(3) |
[1, 'jun', 2, 2] | 移除列表中某个值的第一个匹配项 |
list1.reverse() |
[2, 2, 'jun', 1] | 对列表的元素进行反向排列 |
list1.sort() |
[2, 2, 'jun', 1] | 对原列表进行排序, 若是指定参数,则使用比较函数指定的比较函数 |
用于在列表末尾一次性追加另外一个序列(元组和列表)中的多个值(用新列表扩展原来的列表)spa
list3 = [12, 'as', 45]
list4 = (23, 'ed')
list3.extend(list4)
print(list3)
//输出:
[12, 'as', 45, 23, 'ed']
复制代码
用于移除列表中的一个元素(默认最后一个元素),而且返回该元素的值线程
list.pop(obj=list[-1])
//使用
list3 = [12, 'as', 45, 23, 'ed']
print(list3)
print(list3.pop())
print(list3)
print(list3.pop(2))
print(list3)
//输出:
ed
[12, 'as', 45, 23]
45
[12, 'as', 23]
复制代码
先回顾一下上篇文章介绍的元组的基础知识code
# 元组
tuple1 = (12, 34, 3.14, 5.3, 'titan')
tuple2 = (10, 'jun')
# 1.完整元组
print(tuple1)
# 2.元组一个元素
print(tuple1[0])
# 3.获取第2-3个元素
print(tuple1[2:3])
# 4.获取第三个到最后的全部元素
print(tuple1[2:])
# 5.获取最后一个元素
print(tuple1[-1])
# 6.获取倒数第二个元素
print(tuple1[-2])
# 7.获取最后三个元素
print(tuple1[-3:-1])
# 8.合并元组
print(tuple1 + tuple2)
# 9.重复操做两次
print(tuple2 * 2)
复制代码
与列表的运算符和操做相似, 以下:orm
# 计算元素个数
print(len((1, 2, 3)))
# 合并元组
tuple1 = (1, 2) + (4, 5)
print(tuple1)
# 重复
tuple2 = ('jun',) * 3
print(tuple2)
# 检测是否包含某元素
if (2 in tuple1):
print('2在该元组内')
else:
print('不在元组内')
# 遍历元组
for x in tuple1:
print(x)
//输出:
3
(1, 2, 4, 5)
('jun', 'jun', 'jun')
2在该元组内
1
2
4
5
复制代码
tuple1 = (1, 2, 4, 5)
# 元组中元素最大值
print(max(tuple1))
# 元组中元素最小值
print(min(tuple1))
# 列表转换为元组
print(tuple(['a', 'd', 'f']))
//输出:
5
1
('a', 'd', 'f')
复制代码
先看看上文中介绍到的字典的相关基础知识, 须要注意的是: 键必须不可变,因此能够用数字,字符串或元组充当,因此用列表就不行htm
# 字典
dict1 = {'name': 'jun', 'age': 18, 'score': 90.98}
dict2 = {'name': 'titan'}
# 完整字典
print(dict2)
# 1.修改或添加字典元素
dict2['name'] = 'brother'
dict2['age'] = 20
dict2[3] = '完美'
dict2[0.9] = 0.9
print(dict2)
# 2.根据键值获取value
print(dict1['score'])
# 3.获取全部的键值
print(dict1.keys())
# 4.获取全部的value值
print(dict1.values())
# 5.删除字典元素
del dict1['name']
print(dict1)
# 6.清空字典全部条目
dict1.clear()
print(dict1)
# 7.删除字典
dict3 = {2: 3}
del dict3
# 当该数组呗删除以后, 在调用会报错
# print(dict3)
复制代码
dic1 = {'name': 'titan', 'age':20}
# 计算字典元素个数,即键的总数
print(len(dic1))
# 字典(Dictionary) str() 函数将值转化为适于人阅读的形式,以可打印的字符串表示
print(str(dic1))
# 返回输入的变量类型,若是变量是字典就返回字典类型
print(type(dic1))
//输出:
2
{'name': 'titan', 'age': 20}
<class 'dict'>
复制代码
copy()
函数返回一个字典的浅复制copy
的区别dict1 = {'user':'runoob','num':[1,2,3]}
dict2 = dict1 # 浅拷贝: 引用对象
dict3 = dict1.copy() # 浅拷贝:深拷贝父对象(一级目录),子对象(二级目录)不拷贝,仍是引用
# 修改 data 数据
dict1['user']='root'
dict1['num'].remove(1)
# 输出
print(dict1)
print(dict2)
print(dict3)
# 输出结果
{'num': [2, 3], 'user': 'root'}
{'num': [2, 3], 'user': 'root'}
{'num': [2, 3], 'user': 'runoob'}
复制代码
实例中 dict2
实际上是 dict1
的引用(别名),因此输出结果都是一致的,dict3
父对象进行了深拷贝,不会随dict1
修改而修改,子对象是浅拷贝因此随 dict1
的修改而修改
fromkeys()
函数用于建立一个新字典,seq
中元素作字典的键value
为字典全部键对应的初始值(可选参数)dict.fromkeys(seq[, value])
# 使用
dic2 = dict.fromkeys(['name', 'titan'])
print(dic2)
dic3 = dict.fromkeys(['name', 'titan'], 20)
print(dic3)
# 输出:
{'name': None, 'titan': None}
{'name': 20, 'titan': 20}
复制代码
get()
函数返回指定键的值,若是值不在字典中返回默认值setdefault()
和get()
方法相似, 若是键不存在于字典中,将会添加键并将值设为默认值(同事也会把键值对添加到字典中)dict.get(key, default=None)
# 使用
dic5 = {'name': 'titan', 'age':20}
print(dic5.get('name'))
print(dic5.get('Sex', 'man'))
print(dic5.setdefault('name'))
print(dic5.setdefault('Sex', 'man'))
print(dic5)
# 输出结果:
titan
man
titan
man
{'name': 'titan', 'age': 20, 'Sex': 'man'}
复制代码
把字典的键/值对更新到另外一个字典里(合并字典)
dict.update(dict2)
# 使用
dic6 = {'sex': 'new'}
dic5.update(dic6)
print(dic5)
# 输出:
{'name': 'titan', 'age': 20, 'Sex': 'man', 'sex': 'new'}
复制代码
pop()
: 删除字典给定键 key 所对应的值,返回值为被删除的值。key值必须给出。 不然,返回default值popitem()
: 随机返回并删除字典中的一对键和值。 若是字典已经为空,却调用了此方法,就报出KeyError异常pop(key[,default])
popitem()
# 使用
print(dic5.pop('Sex'))
print(dic5)
print(dic5.popitem())
print(dic5)
# 输出:
man
{'name': 'titan', 'age': 20, 'sex': 'new'}
('sex', 'new')
{'name': 'titan', 'age': 20}
复制代码
dic2 = {'name': 'titan', 'age':20}
# 判断键是否存在于字典中, 在True, 不在False
print(dic2.__contains__('name'))
# 以列表返回可遍历的(键, 值) 元组数组
print(dic2.items())
# 删除字典内全部元素
dic2.clear()
print(dic2)
# 输出结果:
True
dict_items([('name', 'titan'), ('age', 20)])
{}
复制代码
Python
提供了一个 time
和 calendar
属性 | 描述 | 取值 |
---|---|---|
tm_year | 4位数年 | 2018 |
tm_mon | 月 | 1 到 12 |
tm_mday | 日 | 1到31 |
tm_hour | 小时 | 0 到 23 |
tm_min | 分钟 | 0 到 59 |
tm_sec | 秒 | 0 到 61 (60或61 是闰秒) |
tm_wday | 礼拜几 | 0到6 (0是周一) |
tm_yday | 一年的第几日 | 1 到 366(儒略历) |
tm_isdst | 夏令时 | -1, 0, 1, -1是决定是否为夏令时的旗帜 |
获取时间的简单示例
# 日期和时间
import time
# 当前时间戳
ticks = time.time()
print(ticks)
# 本地时间
localTime = time.localtime()
print(localTime)
# 格式化时间
print(time.asctime(localTime))
# 输出结果:
1524051644.320941
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=18, tm_hour=19, tm_min=40, tm_sec=44, tm_wday=2, tm_yday=108, tm_isdst=0)
Wed Apr 18 19:40:44 2018
复制代码
先看几个简单示例
# 1.格式化日期
# 格式化成 2018-04-18 19:49:44 形式
newDate1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(newDate1)
# 格式化成 Wed Apr 18 19:50:53 2018 形式
newDate2 = time.strftime('%a %b %d %H:%M:%S %Y', time.localtime())
print(newDate2)
# 将时间字符串转化为时间戳
timeNum = time.mktime(time.strptime(newDate2, "%a %b %d %H:%M:%S %Y"))
print(timeNum)
# 输出结果:
2018-04-18 19:52:21
Wed Apr 18 19:52:21 2018
1524052341.0
复制代码
Python
中时间和日期相关的格式化符号
%y
: 两位数的年份表示(00-99)%Y
: 四位数的年份表示(000-9999)%m
: 月份(01-12)%d
: 月内中的一天(0-31)%H
: 24小时制小时数(0-23)%I
: 12小时制小时数(01-12)%M
: 分钟数(00=59)%S
: 秒(00-59)%a
: 本地简化星期名称%A
: 本地完整星期名称%b
: 本地简化的月份名称%B
: 本地完整的月份名称%c
: 本地相应的日期表示和时间表示%j
: 年内的一天(001-366)%p
: 本地A.M.或P.M.的等价符%U
: 一年中的星期数(00-53)星期天为星期的开始%w
: 星期(0-6),星期天为星期的开始%W
: 一年中的星期数(00-53)星期一为星期的开始%x
: 本地相应的日期表示%X
: 本地相应的时间表示%Z
: 当前时区的名称%%
: %号自己Time
模块包含了如下内置函数,既有时间处理相的,也有转换时间格式的
timezone
: 当地时区(未启动夏令时)距离格林威治的偏移秒数(>0,美洲;<=0大部分欧洲,亚洲,非洲)tzname
: 包含一对根据状况的不一样而不一样的字符串,分别是带夏令时的本地时区名称,和不带的print(time.timezone)
print(time.tzname)
# 输出结果
-28800
('CST', 'CST')
复制代码
返回格林威治西部的夏令时地区的偏移秒数。若是该地区在格林威治东部会返回负值(如西欧,包括英国)。对夏令时启用地区才能使用
print(time.altzone)
# 输出结果:
-28800
复制代码
接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"
(2008年12月11日 周二18时07分14秒)的24个字符的字符串
localTime = time.localtime()
print(localTime)
# 格式化时间
print(time.asctime(localTime))
# 输出结果:
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=18, tm_hour=19, tm_min=40, tm_sec=44, tm_wday=2, tm_yday=108, tm_isdst=0)
Wed Apr 18 19:40:44 2018
复制代码
ctime
: 把一个时间戳(按秒计算的浮点数)转化为time.asctime()
的形式。gmtime
: 将一个时间戳转换为UTC时区(0时区)的struct_time
(struct_time是在time
模块中定义的表示时间的对象)localtime
: 相似gmtime
,做用是格式化时间戳为本地的时间time.time()
为参数time.ctime([ sec ])
time.gmtime([ sec ])
time.localtime([ sec ])
# 使用
print(time.ctime())
print(time.ctime(time.time() - 100))
print(time.gmtime())
print(time.gmtime(time.time() - 100))
print(time.localtime())
print(time.localtime(time.time() - 100))
# 输出结果:
Wed Apr 18 20:18:19 2018
Wed Apr 18 20:16:39 2018
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=18, tm_hour=12, tm_min=25, tm_sec=44, tm_wday=2, tm_yday=108, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=18, tm_hour=12, tm_min=24, tm_sec=4, tm_wday=2, tm_yday=108, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=9, tm_min=45, tm_sec=19, tm_wday=3, tm_yday=109, tm_isdst=0)
time.struct_time(tm_year=2018, tm_mon=4, tm_mday=19, tm_hour=9, tm_min=43, tm_sec=39, tm_wday=3, tm_yday=109, tm_isdst=0)
复制代码
struct_time
对象做为参数,返回用秒数来表示时间的浮点数OverflowError
或 ValueError
time.mktime(t)
# 使用
t = (2018, 4, 19, 10, 10, 20, 2, 34, 0)
print(time.mktime(t))
print(time.mktime(time.localtime()))
# 输出结果:
1524103820.0
1524104835.0
复制代码
推迟调用线程,可经过参数secs
指秒数,表示进程推迟的时间
time.sleep(t)
# 使用
print(time.ctime())
time.sleep(3)
print(time.ctime())
# 输出结果:
Thu Apr 19 10:29:51 2018
Thu Apr 19 10:29:54 2018
复制代码
format
决定, 上面已经简单介绍过了format
-- 格式字符串t
-- 可选的参数t是一个struct_time
对象time.strftime(format[, t])
# 使用
newDate1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
print(newDate1)
# 输出结果:
2018-04-19 10:35:22
复制代码
time.strptime(string[, format])
# 使用
structTime= time.strptime('20 Nov 2018', '%d %b %Y')
print(structTime)
# 输出结果:
time.struct_time(tm_year=2018, tm_mon=11, tm_mday=20, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=324, tm_isdst=-1)
复制代码
根据环境变量TZ从新初始化时间相关设置, 标准TZ环境变量格式:
std offset [dst [offset [,start[/time], end[/time]]]]
复制代码
std
和 dst
: 三个或者多个时间的缩写字母。传递给 time.tzname
.offset
: 距UTC
的偏移,格式: [+|-]hh[:mm[:ss]] {h=0-23, m/s=0-59}
。start[/time]
, end[/time]
: DST
开始生效时的日期。格式为 m.w.d
— 表明日期的月份、周数和日期。w=1
指月份中的第一周,而 w=5
指月份的最后一周。start
和 end
能够是如下格式之一:
Jn
: 儒略日 n (1 <= n <= 365)
。闰年日(2月29)不计算在内。n
: 儒略日 (0 <= n <= 365)
。 闰年日(2月29)计算在内Mm.n.d
: 日期的月份、周数和日期。w=1
指月份中的第一周,而 w=5
指月份的最后一周。time
:(可选)DST
开始生效时的时间(24 小时制)。默认值为 02:00(指定时区的本地时间)# 没有返回值
time.tzset()
# 使用
import time
import os
os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0'
time.tzset()
print time.strftime('%X %x %Z')
os.environ['TZ'] = 'AEST-10AEDT-11,M10.5.0,M3.5.0'
time.tzset()
print time.strftime('%X %x %Z')
# 输出结果为:
13:00:40 02/17/09 EST
05:00:40 02/18/09 AEDT
复制代码
Calendar
模块的相关函数# 返回当前每周起始日期的设置, 默认状况下,首次载入caendar模块时返回0,即星期一
print(calendar.firstweekday())
# 是闰年返回True,不然为false
# calendar.isleap(year)
print(calendar.isleap(2016))
# 返回在Y1,Y2两年之间的闰年总数
# calendar.leapdays(y1,y2)
print(calendar.leapdays(2015, 2021))
# 返回一个元组, 第一个元素是该月的第一天是星期几(0-6, 0是星期日), 第二个元素是该月有几天
# calendar.monthcalendar(year,month)
print(calendar.monthrange(2018, 4))
# 返回给定日期是星期几(0-6, 0是星期一)
# calendar.weekday(year,month,day)
print(calendar.weekday(2018, 4, 19))
# 设置每周的起始日期
# calendar.setfirstweekday(weekday) 无返回值
calendar.setfirstweekday(3)
print(calendar.firstweekday())
# 输出结果:
0
True
2
(6, 30)
3
3
复制代码
calendar
和 prcal
方法返回一个多行字符串格式的year年年历,3个月一行,间隔距离为c
。 每日宽度间隔为w
字符。每行长度为21* W+18+2* C
。l
是每星期行数
calendar.calendar(year,w=2,l=1,c=6)
calendar.prcal(year,w=2,l=1,c=6)
//使用
year18 = calendar.calendar(2018)
print(year18)
print(calendar.prcal(2018))
复制代码
month
和 prmonth
方法返回一个多行字符串格式的year
年month
月日历,两行标题,一周一行。每日宽度间隔为w
字符。每行的长度为7* w+6
。l
是每星期的行数
calendar.month(year,month,w=2,l=1)
calendar.prmonth(year,month,w=2,l=1)
//使用
monthTime = calendar.month(2018, 4)
print(monthTime)
print(calendar.prmonth(2018, 4))
复制代码
timegm
方法和time.gmtime
相反:接受一个时间元组形式,返回该时刻的时间戳(1970纪元后通过的浮点秒数)
calendar.timegm(tupletime)
# 使用
print(calendar.timegm(time.localtime()))
# 输出结果
1524150128
复制代码
Python
相关的数据类型(数字, 字符串, 元组, 列表和字典)基本都介绍完毕了Python
中的经常使用的时间格式和时间相关的模块(time
和calendar
)也都介绍完了Python
中,其余处理日期和时间的模块还有:datetime模块 和 dateutil模块Python
相关文章后期会持续更新......