标签:索引 ocs rev 方便 stat als 字符 ast super
以下代码示例用的是ipython,一个比官方解释器好很多的解释器,值的学习和使用。
In [18]: abs(3.14) Out[18]: 3.14 In [19]: abs(-3.14) Out[19]: 3.14
In [135]: complex(1,3)
Out[135]: (1+3j)
In [143]: divmod(12, 7)
Out[143]: (1, 5)
In [157]: max([(1,2,3), (4,5,6), (23,4,1,)], key=lambda a: a[-1]) Out[157]: (4, 5, 6) In [158]: max(1,2,3,4,4,5) Out[158]: 5 In [159]: max([(1,2,3), (4,5,6), (23,4,1,)]) Out[159]: (23, 4, 1) In [160]: max([(1,2,3), (4,5,6), (23,4,1,)], key=lambda a: a[-1]) Out[160]: (4, 5, 6) In [161]: max([{‘age‘:10, ‘name‘: ‘aaa‘}, {‘age‘: 12, ‘name‘: ‘bb‘}], key=lambda a: a[‘age‘]) Out[161]: {‘age‘: 12, ‘name‘: ‘bb‘}
In [166]: pow(2,3) Out[166]: 8 In [167]: pow(2,3,5) Out[167]: 3
In [170]: round(3.45) Out[170]: 3.0 In [171]: round(3.55) Out[171]: 4.0 In [172]: round(3.55345, 3) Out[172]: 3.553
In [175]: sum([1,2,3,4])
Out[175]: 10
In [204]: print bin(20), hex(16), oct(9) 0b10100 0x10 011
In [184]: print bool(3), bool(‘a‘) True True In [185]: print bool(0), bool(‘‘), bool(None) False False False
In [188]: chr(320) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-188-5b2996ffe50c> in <module>() ----> 1 chr(320) ValueError: chr() arg not in range(256) In [189]: chr(65) Out[189]: ‘A‘ In [190]: chr(0) Out[190]: ‘\x00‘
In [225]: unichr(1245) Out[225]: u‘\u04dd‘
In [192]: ord(‘a‘) Out[192]: 97 In [193]: ord(‘\x23‘) Out[193]: 35
In [196]: print float(‘13‘), float(13) 13.0 13.0 In [197]: print int(‘14‘), int(14) 14 14 In [198]: print long(‘15‘), long(15) 15 15
In [212]: format(123, ‘05d‘) Out[212]: ‘00123‘
以上等同于 print ‘%05d’ % 123
In [218]: hash(123) Out[218]: 123 In [219]: hash(‘abc‘) Out[219]: 1453079729188098211
In [221]: str(123) Out[221]: ‘123‘ In [222]: str([1,2,3]) Out[222]: ‘[1, 2, 3]‘ In [223]: str({‘a‘: 1, ‘b‘: 2}) Out[223]: "{‘a‘: 1, ‘b‘: 2}"
In [251]: file(‘abc.txt‘, ‘w‘) Out[251]: <open file ‘abc.txt‘, mode ‘w‘ at 0x7f93e727a660> In [252]: open(‘abc.txt‘, ‘w‘) Out[252]: <open file ‘abc.txt‘, mode ‘w‘ at 0x7f93e727a780>
In [253]: input(‘pls input a number >>‘) pls input a number >>123 Out[253]: 123
In [255]: all([1,2,3,4]) Out[255]: True In [256]: all([1,2,3,4, 0]) Out[256]: False In [257]: any([1,2,3,4, 0]) Out[257]: True
In [261]: for i, value in enumerate([‘a‘, ‘b‘, ‘c‘]): .....: print i, value .....: 0 a 1 b 2 c
In [263]: filter(lambda x: x>3, [1,2,3,4,5]) Out[263]: [4, 5]
读取文件的时候比较有用:
with open("mydata.txt") as fp: for line in iter(fp.readline, "STOP"): process_line(line)
In [267]: len(‘abc‘), len([1,2,3]) Out[267]: (3, 3)
In [269]: map(lambda x: x+3, [1,2,3]) Out[269]: [4, 5, 6] In [270]: a = [1,2]; b = [‘a‘, ‘b‘]; c = (‘x‘, ‘y‘) In [271]: map(None, a, b, c) Out[271]: [(1, ‘a‘, ‘x‘), (2, ‘b‘, ‘y‘)]
In [281]: reduce(lambda a, b: a-b, [1,2,3]) Out[281]: -4
In [283]: zip([1,2,3], (‘a‘, ‘b‘, ‘c‘)) Out[283]: [(1, ‘a‘), (2, ‘b‘), (3, ‘c‘)]
In [274]: [x for x in xrange(10)] Out[274]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [275]: [x for x in xrange(5, 10)] Out[275]: [5, 6, 7, 8, 9] In [276]: [x for x in xrange(5, 10, 2)] Out[276]: [5, 7, 9] In [277]: range(10) Out[277]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [278]: range(5, 10) Out[278]: [5, 6, 7, 8, 9] In [279]: range(5, 10, 2) Out[279]: [5, 7, 9]
可选参数cmp、key和reverse与list.sort()方法的参数含义相同(在可变的序列类型一节描述)。
cmp指定一个自定义的带有两个参数的比较函数(可迭代的元素),它应该根据第一个参数是小于、等于还是大于第二个参数返回负数、零或者正数:cmp=lambda x,y: cmp(x.lower(), y.lower())。默认值是None。
key指定一个带有一个参数的函数,它用于从每个列表元素选择一个比较的关键字:key=str.lower。默认值是None(直接比较元素)。
reverse是一个布尔值。如果设置为True,那么列表元素以反向比较排序。
通常情况下,key和reverse转换处理比指定一个等同的cmp函数要快得多。这是因为cmp为每个元素调用多次但是key和reverse只会触摸每个元素一次。使用functools.cmp_to_key()来转换旧式的cmp函数为key函数。
In [288]: sorted(d.items(), key=lambda a: a[1]) Out[288]: [(‘a‘, 3), (‘b‘, 4)] In [289]: sorted(d.items(), key=lambda a: a[1], rev) In [289]: sorted(d.items(), key=lambda a: a[1], reverse=True) Out[289]: [(‘b‘, 4), (‘a‘, 3)] In [290]: sorted(d.items(), cmp=lambda a, b: cmp(a[1], b[1])) Out[290]: [(‘a‘, 3), (‘b‘, 4)]
bytearray() dict() frozenset() list() set() tuple()
python里面常用的数据结构有列表(list)、字典(dict)、集合(set)、元组(tuple)
以下是一些类(class)和类型相关的函数,比较不常用,可以查看手册详细了解。
basestring() callable() classmethod() staticmethod() property() cmp() compile() delattr() getattr() setattr() hasattr() dir() globals() locals() vars() help() id() isinstance() issubclass() object() memoryview() repr() super() type() unicode() import() eval() execfile()
apply() buffer() coerce() intern()
ipython是一个非常好的交互式python解释器,它查看一个函数或类的用法的方法有:
In [292]: import time In [293]: time. time.accept2dyear time.clock time.gmtime time.sleep time.struct_time time.tzname time.altzone time.ctime time.localtime time.strftime time.time time.tzset time.asctime time.daylight time.mktime time.strptime time.timezone In [293]: time.ti time.time time.timezone In [293]: time.time? Docstring: time() -> floating point number Return the current time in seconds since the Epoch. Fractions of a second may be present if the system clock provides them. Type: builtin_function_or_method
文章来源于python学习笔记:www.yuanrenxue.com
标签:索引 ocs rev 方便 stat als 字符 ast super
原文地址:https://www.cnblogs.com/amiza/p/10347649.html