码迷,mamicode.com
首页 > 编程语言 > 详细

python基础3—迭代器 | 切片

时间:2018-01-22 17:31:25      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:sim   ati   for循环   except   iter   style   exce   java   概念   

这里有很多python特有的东西,非常有意思,以前只接触过C, C++, Java, Javascript,没想到还可以这样玩

# ------------------slice-------------------- #
# slice 切片 从数组切出另一个数组
li = list(range(10))
print(li)   # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# [start, end, step]  || (start - end) 要和 step的正负号一致

print(li[2:5])    # [2, 3, 4]
print(li[:4])    # [0, 1, 2, 3]
print(li[5:])    # [5, 6, 7, 8, 9]
print(li[0:20:3])        # [0, 3, 6, 9]


# how about minus
print(li[5:-2])            # [5, 6, 7]
print(li[9:0:-1])    # [9, 8, 7, 6, 5, 4, 3, 2, 1]
print(li[9:0:1])    # []
print(li[9::-1])    # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(li[::-2])    # [9, 7, 5, 3, 1]

# a new object
print(li)
re_li = li[::-1]            # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(re_li)


# ------------------comprehension-------------------- #
# comprehension  推导列表
# simple case
li = []
for i in range(10):
    li.append(i) 
print(li)

li = list(range(10)):
        print(li)

li  = [1] * 10
print(li)

# 浅拷贝
li_2d = [[0] * 3 ] * 3
print(li_2d)

li_2d[0][0] = 100
print(li_2d)

#深拷贝
li_2d = [ [0] * 3 for i in range(3)]
print(li_2d)

li_2d[0][0] = 100
print(li_2d)

li = (x for x in range(10))
print(type(li))         # generator
print(li)                  # generator object
      
for i in range(10):  # way1 
      print(next(li))
      
for i in li:                # way2
      print(i)

      
li = [x for x in range(10)]    
print(type(li))         # list
print(li)                  # [1, 2, 3, 4]

li =  {x for x in range(3)}
print(type(li))         # set
print(li)                  # {0, 1, 2}

s = {x for x in range(10)  if x%2==0 }
print(type(s))     # set
print(s)              # {0, 8, 2, 4, 6}

s = [ x%2==0 for x in range(10)]
print(type(s))     # list
print(s)              # [True, False, Ture, False,Ture, False, Ture, False, Ture, False]

d = {x: x % 2 == 0 for x in range(10)}
print(type(d))    # list
print(d)             # {0: True, 1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False}

# so  ‘x for x in range(10)‘  is a comprehension


# generator 将真正的计算推迟到使用时  不一次性生成很多元素,省内存
# 2.7 版本时一次性生成100W个数字,在3.5版本并不是真正生成100W个数字而是在next取值时才生成
print(type(range(10)))   # type  

# 平方表
square_table = []
for i in range(50000):
    square_table.append(i * i)
for i in range(5):
    print(square_table[i])

square_generator = ( x * x for x in range(50000))
print(type(square_generator))   # generator

for i in range(5):
    print(next(square_generator))

def fib(limit):
    n, a, b = 0 , 0 , 1
    while n < limit:
        yield b
        a, b  = b, a + b
        n += 1
    pass
import traceback

f = fib(5)
print(type(f))
print(next(f))
print(next(f))
print(next(f))
print(next(f))
print(next(f))
try: 
    print(next(f))
except StopIteration:
    traceback.print_exc()
for i in fib(5):
    print(i)


# Iterable  Iterator
# 可迭代 和 迭代器不一样的概念, 可迭代表示可以用for循环, 而迭代器是用来使用next()不断返回下一个值,采用惰性计算
# 生成器一定是迭代器  使用一个生成一个 看下面的fib例子
from collections import Iterable
from collections import Iterator

print(isinstance([1,2,3], Iterable))                    # True
print(isinstance({}, Iterable))            # True
print(isinstance(123, Iterable))        # False
print(isinstance(abc, Iterable))        # True

print(isinstance([1, 2, 3], Iterator))                         # False
 
g = (x * x for x in range(10))
print(type(g))        # <type ‘generator‘> 
print(isinstance(g, Iterable))    # True
print(isinstance(g, Iterator))    # True
for i in g:
    print(i)

def fib(limit):
    n, a, b = 0 , 0 , 1
    while n < limit:
        yield b
        a, b  = b, a + b
        n += 1
    pass
f = fib(5)
print(type(f))
print(isinstance(f, Iterable))   # True
print(isinstance(f, Iterator))    # True
for i in f:
    print(i)

 

python基础3—迭代器 | 切片

标签:sim   ati   for循环   except   iter   style   exce   java   概念   

原文地址:https://www.cnblogs.com/zeroones/p/8329491.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!