论坛首页 编程语言技术论坛

python基础

浏览 3347 次
锁定老帖子 主题:python基础
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-07-31  
#-----在字典中循环
dict1={'name':'caiquan','sex':'nan','age':'24','state':'1'}
for k,b in enumerate(dict1.iteritems()):
    print k,b 
#-----在序列中循环
seq1=['tea','apple','peak','banan']
for m in enumerate(seq1):
    print m
#-----在多个序列中循环
question=['name','sex','age','state']
answer=['caiquan','nan','24','0']
anth=['22','33','44','55']
for q,n,a in zip(question,answer,anth):
    print 'question is %s , answer is %s, age is %s'% (q,n,a)
#--(1,10)之间倒序排序,间隔为2
for i in reversed(xrange(1,10,2)):    
    print i
#--无重复元组
xulie=['aa','bb','cc','dd','ee','aa','bb']
for f in sorted(set(xulie)):
    print f

 输出结果:

0 ('age', '24')
1 ('state', '1')
2 ('name', 'caiquan')
3 ('sex', 'nan')
(0, 'tea')
(1, 'apple')
(2, 'peak')
(3, 'banan')
question is name , answer is caiquan, age is 22
question is sex , answer is nan, age is 33
question is age , answer is 24, age is 44
question is state , answer is 0, age is 55
9
7
5
3
1
aa
bb
cc
dd
ee
 
   发表时间:2011-08-11  
tuple、dict、for循环及一些基本API,果然是基础,LZ老实人,貌似Python的东西还有很多嘀,楼主加油。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics