python基础3—迭代器 | 切片

Posted zeroones

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础3—迭代器 | 切片相关的知识,希望对你有一定的参考价值。

这里有很多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—迭代器 | 切片的主要内容,如果未能解决你的问题,请参考以下文章

Python的高级特性(切片,迭代,生成器,迭代器)

Python3 高级特性

《Python学习之路 -- Python基础之切片》

Python 基础 -2.4.3 迭代器

如何对迭代器做切片操作?

Python12 切片迭代