1. 程式人生 > >Python列表操作自定義函式(二)

Python列表操作自定義函式(二)

<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>