<span style="font-size:14px;"># -*- coding: cp936 -*-
#list函数形参传递
s = "www.baidu.com"
print s
li1 = list(s)
print 'li1',li1
#定义一个函数来剔除字符串中的.
def count_char(listchars):
while "." in listchars:
r = listchars.pop(listchars.index('.'))
print r
n = len(listchars)
print 'there are no \'.\' in list'
return n
new_char = count_char(li1)
print 'in %s has %d char' % (s,new_char)
def count_remove(listchars):
while '.' in listchars:
r = listchars.remove('.')
else:
print 'there are no \'.\' in list'
n = len(listchars)
return n
new_char1 = count_remove(li1)
print 'in %s has %d char' % (s,new_char1)
fw = open('E:\pythonRead.txt','a')
li2 = [1,2,3,4,12.3,'nihao','smile']
i = 0
while i <= len(li2) - 1:
if isinstance(li2[i],int):
fw.write(str(li2[i])+'\n')
elif isinstance(li2[i],float):
fw.write(str(li2[i])+'\n')
else:
fw.write(li2[i]+'\n')
i = i + 1
fw.close
</span>
Python列表操作自定义函数(二)
最新推荐文章于 2024-07-16 13:58:30 发布