Python Learning: 03
an inch is worth a pound of gold, an inch of gold is hard to buy an inch of time.
- slice
when the scale of data is so large that we have to pick a short of the content, the best way is to use slice.
-
- forward slice
>>>l=range(1,101) >>>l[1:] #until the end >>>l[:101] #from the first >>>l[1:100] >>>l[1:101:2] #pick one in two
-
- reverse slice
>>>l=range(1,101) >>>l[-1:] >>>l[:-1] >>>l[-5:-1] >>>l[-46::5]
- iteration
in python, iterations are done with "for ... in ...". 'for' loop in python can be used not only on lists and tuples, but alse on any other iterable objects. therefore, the iterative operation is for a collection, whether the collection is ordered or unordered, we can always use the loop to extract each element of the collection in turn. the collection is a data structure containing a set of elements, and we have learnt list, tuple, set, dict, str, unicode.
-
- index iteration: using enumerate() to get the tuple of index and name and using zip() to conbine two lists into one
for index, name in enumerate(l): print index, '-', name
>>> zip([10, 20, 30], ['a', 'b', 'c']) [(10, 'a'), (20, 'b'), (30, 'c')]
-
- the value of dict: values() and itervalues()
- the key-value of dict: items() and iteritems()
上一篇: 华为领衔 MWC2019前瞻:5G/可折叠手机来了
下一篇: 通用查询类封装之Mongodb篇