迭代--Iteration

#python的for循环可用于任何可迭代对象,如:list,tuple,dict,字符串
L1=[1,2,'wxy','gjh']

for l in L1:
print('list迭代:{}'.format(l))

#对list的下标循环操作,Python内置的enumerate函数可以把一个list变成索引-元素对,这样就可以在for循环中同时迭代索引和元素本身
for i,value in enumerate(L1):
print(i,value)

T1=(3,4,5,6)

for t in T1:
print('tuple迭代:{}'.format(t))

str='hellowxy'

for s in str:
print('str迭代:{}'.format(s))

D1={'wxy':90,'gjh':73,'hmm':65}

#迭代dict的key
for dkey in D1:
print('dict的迭代默认为迭代key:{}'.format(dkey))

#迭代value
for dvalue in D1.values():
print('迭代value:{}'.format(dvalue))

#key和value一起迭代
for dkey,dvalue in D1.items():
print('迭代key和value:{0}-->{1}'.format(dkey,dvalue))

#判断是否为可迭代对象,方法是通过collections模块的Iterable类型判断
from collections import Iterable

#isinstance用法:http://www.cnblogs.com/sweet521/p/3976634.html
print(isinstance(123,Iterable))
print(isinstance([1,2,3],Iterable))

转载于:https://www.cnblogs.com/wangxy92/p/7478858.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值