当前位置: 移动技术网 > IT编程>脚本编程>Python > Python Learning: 03

Python Learning: 03

2019年02月12日  | 移动技术网IT编程  | 我要评论

故事会在线阅读,殷悦老公,松武漫画

    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()

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网