关闭

Python 的一些高级特性

标签: python
373人阅读 评论(0) 收藏 举报
分类:

Python 的一些高级特性

切片 slice

# coding=utf-8
__author__ = 'Z'

li = [10, 11, 12, 13, 14, 15]
print li[1:2]
print li[-3:-1]


运行结果:
[11]
[13, 14]

迭代 iter

Python中的迭代是通过 for..in 实现的。

列表推导 list comprehension

例子:

# coding=utf-8
__author__ = 'Z'

result = ["0x%02x" % x for x in range(256) if x % 2 != 0]
print result


运行结果:
['0x01', '0x03', '0x05', '0x07', '0x09', '0x0b', '0x0d', '0x0f', '0x11', '0x13', '0x15', '0x17', '0x19', '0x1b', '0x1d', '0x1f', '0x21', '0x23', '0x25', '0x27', '0x29', '0x2b', '0x2d', '0x2f', '0x31', '0x33', '0x35', '0x37', '0x39', '0x3b', '0x3d', '0x3f', '0x41', '0x43', '0x45', '0x47', '0x49', '0x4b', '0x4d', '0x4f', '0x51', '0x53', '0x55', '0x57', '0x59', '0x5b', '0x5d', '0x5f', '0x61', '0x63', '0x65', '0x67', '0x69', '0x6b', '0x6d', '0x6f', '0x71', '0x73', '0x75', '0x77', '0x79', '0x7b', '0x7d', '0x7f', '0x81', '0x83', '0x85', '0x87', '0x89', '0x8b', '0x8d', '0x8f', '0x91', '0x93', '0x95', '0x97', '0x99', '0x9b', '0x9d', '0x9f', '0xa1', '0xa3', '0xa5', '0xa7', '0xa9', '0xab', '0xad', '0xaf', '0xb1', '0xb3', '0xb5', '0xb7', '0xb9', '0xbb', '0xbd', '0xbf', '0xc1', '0xc3', '0xc5', '0xc7', '0xc9', '0xcb', '0xcd', '0xcf', '0xd1', '0xd3', '0xd5', '0xd7', '0xd9', '0xdb', '0xdd', '0xdf', '0xe1', '0xe3', '0xe5', '0xe7', '0xe9', '0xeb', '0xed', '0xef', '0xf1', '0xf3', '0xf5', '0xf7', '0xf9', '0xfb', '0xfd', '0xff']

generator


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:65260次
    • 积分:1644
    • 等级:
    • 排名:千里之外
    • 原创:79篇
    • 转载:3篇
    • 译文:11篇
    • 评论:1条
    最新评论