标签:eve 元素 取出 opened 对象 tin 字符串转换 元组 condition
1.另外一种格式化方法:
template = ‘{0:.2f} {1:s} are worth US${2:d}‘ {0:.2f}表示格式化第?个参数为带有两位?数的浮点数。 {1:s}表示格式化第?个参数为字符串。 {2:d}表示格式化第三个参数为?个整数。 template.format(4.5560, ‘Argentine Pesos‘, 1)
2.encode和decode
假如知道字符编码,可以将其转化为Unicode。看?个例?: val = "español" val_utf8 = val.encode(‘utf-8‘) val_utf8 b‘espa\xc3\xb1ol‘ type(val_utf8) bytes 反之亦可
3.有关时间
1. In [102]: from datetime import datetime, date, time In [103]: dt = datetime(2011, 10, 29, 20, 30, 21) In [104]: dt.day Out[104]: 29 In [105]: dt.minute Out[105]: 30 2. 根据datetime实例,你可以?date和time提取出各?的对象: In [106]: dt.date() Out[106]: datetime.date(2011, 10, 29) In [107]: dt.time() Out[107]: datetime.time(20, 30, 21) 3. strftime?法可以将datetime格式化为字符串: In [108]: dt.strftime(‘%m/%d/%Y %H:%M‘) Out[108]: ‘10/29/2011 20:30‘ strptime可以将字符串转换成datetime对象: In [109]: datetime.strptime(‘20091031‘, ‘%Y%m%d‘) Out[109]: datetime.datetime(2009, 10, 31, 0, 0) 4. 当你聚类或对时间序列进?分组,替换datetimes的time字段有时 会很有?。例如,?0替换分和秒: In [110]: dt.replace(minute=0, second=0) Out[110]: datetime.datetime(2011, 10, 29, 20, 0) 5. 两个datetime对象的差会产??个datetime.timedelta类型: In [111]: dt2 = datetime(2011, 11, 15, 22, 30) In [112]: delta = dt2 - dt In [113]: delta Out[113]: datetime.timedelta(17, 7179) In [114]: type(delta) Out[114]: datetime.timedelta 结果timedelta(17, 7179)指明了timedelta将17天、7179秒的编码?式。 将timedelta添加到datetime,会产??个新的偏 In [115]: dt Out[115]: datetime.datetime(2011, 10, 29, 20, 30, 21) In [116]: dt + delta Out[116]: datetime.datetime(2011, 11, 15, 22, 30)
4.三元表达式
Python中的三元表达式可以将if-else语句放到???。语法如 下: value = true-expr if condition else false-expr 例子: In [126]: x = 5 In [127]: ‘Non-negative‘ if x >= 0 else ‘Negative‘ Out[127]: ‘Non-negative‘
5.元组
元组是?个固定?度,不可改变的Python序列对象。创建元组的 最简单?式,是?逗号分隔?列值: In [1]: tup = 4, 5, 6 In [2]: tup Out[2]: (4, 5, 6) ?tuple可以将任意序列或迭代器转换成元组: In [5]: tuple([4, 0, 2]) Out[5]: (4, 0, 2) In [6]: tup = tuple(‘string‘) In [7]: tup Out[7]: (‘s‘, ‘t‘, ‘r‘, ‘i‘, ‘n‘, ‘g‘) In [8]: tup[0] Out[8]: ‘s‘ 可以?加号运算符将元组串联起来: In [13]: (4, None, ‘foo‘) + (6, 0) + (‘bar‘,) Out[13]: (4, None, ‘foo‘, 6, 0, ‘bar‘) 2. 拆分元组 如果你想将元组赋值给类似元组的变量,Python会试图拆分等号 右边的值: In [15]: tup = (4, 5, 6) In [16]: a, b, c = tup In [17]: b Out[17]: 5 即使含有元组的元组也会被拆分: In [18]: tup = 4, 5, (6, 7) In [19]: a, b, (c, d) = tup In [20]: d Out[20]: 7 3.变量拆分常?来迭代元组或列表序列: In [27]: seq = [(1, 2, 3), (4, 5, 6), (7, 8, 9)] In [28]: for a, b, c in seq: ....: print(‘a={0}, b={1}, c={2}‘.format(a, b, c)) a=1, b=2, c=3 a=4, b=5, c=6 a=7, b=8, c=9 4.允许从元组的开 头“摘取”?个元素。它使?了特殊的语法*rest,这也?在函数 签名中以抓取任意?度列表的位置参数: In [29]: values = 1, 2, 3, 4, 5 In [30]: a, b, *rest = values In [31]: a, b Out[31]: (1, 2) In [32]: rest Out[32]: [3, 4, 5] 5.count() In [34]: a = (1, 2, 2, 2, 3, 4, 2) In [35]: a.count(2) Out[35]: 4
6.列表
1.insert,append In [45]: b_list.append(‘dwarf‘) In [46]: b_list Out[46]: [‘foo‘, ‘peekaboo‘, ‘baz‘, ‘dwarf‘] In [47]: b_list.insert(1, ‘red‘) In [48]: b_list Out[48]: [‘foo‘, ‘red‘, ‘peekaboo‘, ‘baz‘, ‘dwarf‘] 2.insert的逆运算是pop,它移除并返回指定位置的元素: In [49]: b_list.pop(2) Out[49]: ‘peekaboo‘ In [50]: b_list Out[50]: [‘foo‘, ‘red‘, ‘baz‘, ‘dwarf‘] 3.可以?remove去除某个值,remove会先寻找第?个值并除去: In [51]: b_list.append(‘foo‘) In [52]: b_list Out[52]: [‘foo‘, ‘red‘, ‘baz‘, ‘dwarf‘, ‘foo‘] In [53]: b_list.remove(‘foo‘) In [54]: b_list Out[54]: [‘red‘, ‘baz‘, ‘dwarf‘, ‘foo‘] 4.如果已经定义了?个列表,?extend?法可以追加多个元素: In [58]: x = [4, None, ‘foo‘] In [59]: x.extend([7, 8, (2, 3)]) In [60]: x Out[60]: [4, None, ‘foo‘, 7, 8, (2, 3)] 5.切? ?切边可以选取?多数序列类型的?部分,切?的基本形式是在 ?括号中使?start:stop: In [73]: seq = [7, 2, 3, 7, 5, 6, 0, 1] In [74]: seq[1:5] Out[74]: [2, 3, 7, 5] 切?也可以被序列赋值: In [75]: seq[3:4] = [6, 3] In [76]: seq Out[76]: [7, 2, 3, 6, 3, 5, 6, 0, 1] ?个聪明的?法是使?-1,它可以将列表或元组颠倒过来: In [82]: seq[::-1] Out[82]: [1, 0, 6, 5, 3, 6, 3, 2, 7]
标签:eve 元素 取出 opened 对象 tin 字符串转换 元组 condition
原文地址:https://www.cnblogs.com/lianghaiming/p/12517310.html