2020-2-4(117)

>>> a=[1,2,3]
>>> it=iter(a)
>>> print(it)
<list_iterator object at 0x000001AC6F61AB08>
>>> print(it.__next__())
1
>>> print(it.__next__())
2
>>> print(it.__next__())
3
>>> print(it.__next__())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> a=[1,2,3,4,5]
>>> a=iter(a)
>>> next(a)
1
>>> next(a)
2
>>> next(a)
3
>>> next(a)
4
>>> next(a)
5
>>>
>>> try:                 
...     next(a)
... except StopIteration:
...     print("没有元素可以用了")
...
没有元素可以用了
class MyRange(object):
    def __init__(self, n):
        self.idx = 0
        self.n = n

    def __iter__(self):
        return self
 
    def __next__(self):
        if self.idx < self.n:
            val = self.idx
            self.idx += 1
            return val
        else:
            raise StopIteration()

myRange = MyRange(3)
print (next(myRange))
print (next(myRange))
print (next(myRange))

class MyRange(object):
    def __init__(self, n):
        self.idx = 0
        self.n = n

    def __iter__(self):
        return self
 
    def __next__(self):
        try:
            if self.idx < self.n:
                val = self.idx
                self.idx += 1
                return val
        except:
            raise StopIteration()

myRange = MyRange(3)


print (next(myRange))
print (next(myRange))
print (next(myRange))
print (next(myRange))
print("************")

def odd():
    print ('step 1')
    yield 1
    print ('step 2')
    yield 3
    print ('step 3')
    yield 5

o = odd()
print (next(o))
print (next(o))
print (next(o))

for i in o:
    print (i)   #可以遍历执行函数

>>> a=[1,2,3,[4,5]]
>>> b=a
>>> import copy
>>> c=copy.copy(a)
>>> d=copy.deepcopy(a)
>>> a.append("a")
>>> a
[1, 2, 3, [4, 5], 'a']
>>> b
[1, 2, 3, [4, 5], 'a']
>>> c
[1, 2, 3, [4, 5]]
>>> d
[1, 2, 3, [4, 5]]
>>> a[3].append("b")
>>> a
[1, 2, 3, [4, 5, 'b'], 'a']
>>> b
[1, 2, 3, [4, 5, 'b'], 'a']
>>> c
[1, 2, 3, [4, 5, 'b']]
>>> d
[1, 2, 3, [4, 5]]
>>> a=["a",[1,2]]
>>> b=copy.copy(a)
>>> c=copy.deepcopy(a)
>>> id(a)
2689090637064
>>> id(b)
2689090637128
>>> id(c)
2689089896072
>>> id(a[0])
2689089727536
>>> id(b[0])
2689089727536
>>> id(c[0])
2689089727536
>>> id(a[1])
2689090634056
>>> id(b[1])
2689090634056
>>> id(c[1])
2689090637320
>>> a[0]=10
>>> a
[10, [1, 2]]
>>> b
['a', [1, 2]]
>>> c
['a', [1, 2]]
>>> a.append("c")
>>> a
[10, [1, 2], 'c']
>>> b
['a', [1, 2]]
>>> c
['a', [1, 2]]
>>> a
[10, [1, 2], 'c']
>>> d=a[:]
>>> a.append("b")
>>> a
[10, [1, 2], 'c', 'b']
>>> d
[10, [1, 2], 'c']
>>> a[1].append(3)
>>> a
[10, [1, 2, 3], 'c', 'b']
>>> d
[10, [1, 2, 3], 'c']

queue=[]
while 1:
    value=input('请输入:')
    if value=='done':
        break
    queue.insert(0,value)
   
while 1:
    if len(queue)>0:
        value=queue.pop()
        print('get value:%s' %value)
    else:
        break

queue=[]
while 1:
    a=input('请输入:')
    if a=='end':
        break
    queue.append(a)
while 1:
    if len(queue)>0:
        print(queue.pop())
    else:
        break
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值