例子1:
打印列表每个元素对应的索引
[[email protected] script]# vim suoyin.py #!/usr/bin/python # coding:utf-8 alist = [‘fu‘,‘shan‘,‘hua‘,‘hive‘] for i in range(len(alist)): print ‘%s:%s‘ % (i,alist[i]) 检验: [[email protected] script]# python suoyin.py 0:fu 1:shan 2:hua 3:hive另外一种方式:
[[email protected] script]# vim suoyin.py #!/usr/bin/python # coding:utf-8 alist = [‘fu‘,‘shan‘,‘hua‘,‘hive‘] #for i in range(len(alist)): # print ‘%s:%s‘ % (i,alist[i]) for i in enumerate(alist): print ‘%s:%s‘ %(i[0],i[1]) 检验: [[email protected] script]# python suoyin.py (0, ‘fu‘) (1, ‘shan‘) (2, ‘hua‘) (3, ‘hive‘)
[[email protected] script]# vim suoyin.py
#!/usr/bin/python
# coding:utf-8
alist = [‘fu‘,‘shan‘,‘hua‘,‘hive‘]
#for i in range(len(alist)):
# print ‘%s:%s‘ % (i,alist[i])
for i in enumerate(alist):
print ‘%s:%s‘ % (i[0],i[1])
检验:
[[email protected] script]# python suoyin.py
0:fu
1:shan
2:hua
3:hive
还可以:
[[email protected] script]# vim suoyin.py #!/usr/bin/python # coding:utf-8 alist = [‘fu‘,‘shan‘,‘hua‘,‘hive‘] for m,n in enumerate(alist): print ‘%s:%s‘ % (m,n) 检验: [[email protected] script]# python suoyin.py 0:fu 1:shan 2:hua 3:hive
#######列表一些简单用法############
In [10]: alist = [23,10,45,33] In [11]: reversed(alist) Out[11]: <listreverseiterator at 0x7f44bd7c3910> In [12]: list(reversed(alist)) Out[12]: [33, 45, 10, 23] In [15]: sorted(alist) #从小到大,升序 Out[15]: [10, 23, 33, 45]
In [16]: list(reversed(sorted(alist))) #降序排列
Out[16]: [45, 33, 23, 10]
或者:
In [18]: sorted(alist)[::-1]
Out[18]: [45, 33, 23, 10]
##########字符串格式化############
In [19]: ‘%10s%s‘ % (‘name‘,‘fush‘) ##‘%10s‘ 右对齐,占是个字符,不够的用空格补上;默认右对齐 Out[19]: ‘ namefush‘ In [20]: ‘%10s %s‘ % (‘name‘,‘fush‘) Out[20]: ‘ name fush‘ In [21]: ‘%-10s %s‘ % (‘name‘,‘fush‘) ##‘%-10s‘ 左对齐,占是个字符,不够的用空格补上
Out[21]: ‘name fush‘
还有一种不常用的用法:
1 In [22]: ‘%*s %*s‘ % (-20,‘name‘,-8,‘fush‘) ##在s前面用*号,在后面用数字代替 2 Out[22]: ‘name fush ‘
格式化案例:
1 [[email protected] script]# vim huamian.py 2 #!/usr/bin/python 3 # coding:utf-8 4 5 6 def get_content(): 7 contents = [] 8 while True: 9 data = raw_input(‘<Enter to quit>: ‘) 10 contents.append(data) 11 if not data: 12 break 13 return contents 14 15 if __name__ == ‘__main__‘: 16 width = 48 17 lines = get_content() 18 lines.pop() 19 print ‘+%s+‘ % (‘#‘ * width) 20 for line in lines: 21 if not len(line)%2: 22 sp_width = (width -len(line)) / 2 23 print ‘+%s%s%s+‘ % (‘ ‘ * sp_width,line,‘ ‘ * sp_width) 24 else: 25 sp_width = (width -len(line)) / 2 26 print ‘+%s%s%s +‘ % (‘ ‘ * sp_width,line,‘ ‘ * sp_width) 27 print ‘+%s+‘ % (‘#‘ * width)
改进版:
1 #!/usr/bin/python 2 # coding:utf-8 3 4 5 def get_content(): 6 contents = [] 7 while True: 8 data = raw_input(‘<Enter to quit>: ‘) 9 if not data: 10 break 11 contents.append(data) 12 return contents 13 14 if __name__ == ‘__main__‘: 15 width = 48 16 lines = get_content() 17 print ‘+%s+‘ % (‘#‘ * width) 18 for line in lines: 19 sp_width , extra = divmod((width -len(line)),2) 20 print ‘+%s%s%s+‘ % (‘ ‘ * sp_width,line,‘ ‘ * (sp_width + extra)) 21 print ‘+%s+‘ % (‘#‘ * width)备注:num1,num2 = divmod(10,2)num1 = 10/2 的商num2 = 10/2 的余数奇数/2 的余数(num2=1)偶数/2 的余数(num2=0)
验证效果:
1 [[email protected] script]# python huamian.py 2 <Enter to quit>: hello 3 <Enter to quit>: world! 4 <Enter to quit>: 5 +################################################+ 6 + hello + 7 + world! + 8 +################################################+
时间: 2024-11-03 01:45:12