file_obj2=open('hello.txt','w')conta='my name is Bb'file_obj2.write(conta)v=file_obj2.readlines()PRint v
輸出不唯一數(shù)1 (這是一個(gè)國(guó)外Python練習(xí)網(wǎng)站上的題目 我其他博文有介紹 )
def checkio(data): from collections import Counter nonunique = Counter(data) - Counter(set(data)) return [x for x in data if x in nonunique]print checkio([1,31,5,13,13,1,1])print counter(set([1,31,5,13,13,1,1]))
#Your optional code here#You can import some modules or create additional functionsdef checkio(data): return [x for x in data if data.count(x) > 1] #Some hints#You can use list.count(element) method for counting.#Create new list with non-unique elements#or remove elements from original list (but it's bad practice for many real cases)#Loop over original list#These "asserts" using only for self-checking and not necessary for auto-testingif __name__ == "__main__": assert isinstance(checkio([1]), list), "The result must be a list" assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example" assert checkio([1, 2, 3, 4, 5]) == [], "2nd example" assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example" assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"
i=1;s=0;while i<=100: print s,i s=s+i i=i+1print 'exit'
#第八課 python自定義函數(shù)預(yù)定義 1.預(yù)定值放在前面 2. 后面賦值可以把先前預(yù)定的值覆蓋#
def test_e(n1,n2=15): print n1 print n2 return n1+n2test = test_e(2)print test
def test_e(val1,val2,val3): print val1 print val2 print val3 return val1+val2+val3test_e(5,23,55)
#python if語(yǔ)句#
#注意使用 raw_input()的時(shí)候 把字符強(qiáng)制轉(zhuǎn)化成 整形#
count =int(raw_input('input your number:'))print countif count>=80: print 'a'elif count>=70: print'b'else : print 'nono'
#字符區(qū)域代碼#
sex=raw_input('put your sex')if sex=='Male': print 'right'else: print 'no'
#python if分支語(yǔ)句表達(dá)式構(gòu)造 非0 即為真#
#關(guān)系表達(dá)式 > < = 邏輯表達(dá)式 and or not
#網(wǎng)絡(luò)刷博客瀏覽量#
import os
import webbrowser as webimport timei=0while i<=10: web.open('http://blog.sina.com.cn/s/blog_9a0d1f710101vm5k.html?tj=1') time.sleep(0.8) os.system('taskkill /F /IM Chrome.exe') print '%d times already run' % i i=i+1
list1=[2,65,'g',5656] #list 數(shù)據(jù)類(lèi)型i=0;for lists in list1: print format(i,'2d'),lists i=i+1
j=0;string ='hello world'list2=list(string) #for str in list2: #string 直接用數(shù)據(jù)也可以 print format(j,'2d'),str; j=j+1;k=0;for k in range(1,101,2): #range 函數(shù) 范圍在 start 到 end-1 最后一位為每個(gè)數(shù)字間隔數(shù) print k;
s1='hello world'
#for循環(huán) 循環(huán)體遍歷文件和元組#
tup = (1,2,3,4,5) #tup 元組for tups in tup: print tups# file.readlines 獲取文件列表 file.readline 獲取文件字符串第一行#記住 open 的用法str =open('python16.py','r').readline()print len(str)for c in open('python16.py','r').readlines(): print c; open('temp.txt','a++').write(r) # 在原目錄下面增加一個(gè)txt文件 在文件里面寫(xiě)入打開(kāi)的文件內(nèi)容
#轉(zhuǎn)移字符串print r'hello/nworld' #r關(guān)閉轉(zhuǎn)義字符串的作用print u'unicode' #unicode 字符串#格式化字符串print 'your age is %d'%(28)
# open('c://temp//temp.txt','a+') 用一個(gè)‘、’可能被認(rèn)為是轉(zhuǎn)義字符
s1='www.hxend's2='.com'print s1,s2 #這樣輸出的話(huà) 中間會(huì)存在一個(gè)空格print s1+s2 #用‘+’連接兩個(gè)字符串#字符串的重復(fù) *s='abcdefg'print s*5
#訪(fǎng)問(wèn)字符串的某個(gè)元素 index索引ch = s[3]print ch#python 切片 slice s[i:j]sub =s[3:5] #從 start 到 end-1print subprint s[:5]print s[3:]print s[-4:-1] #如果發(fā)生錯(cuò)誤 就會(huì)出現(xiàn) 空格表示# s[i:j:k]print s[-1:-4:-1] #倒序輸出 依次減一s2='www.hxend.com'print s2[9:4:-1]print s2[-1:0:-1]print s2[-1::-1]#用for 循環(huán)遍歷整個(gè)字符串s5='www.hxend.com'for ch in s5: print ch;
#字符串的高級(jí)函數(shù)
#isalnum 判斷是否為 數(shù)字或者為字母 s.isalnum() s='wwwhxend55com'print s.isalnum()# s.isalpha 判斷是否為字母s='abcdef'print s.isalpha() # s.isdigit() 判斷是否為數(shù)字s='626611'print s.isdigit()# s.islower s.isupper 判斷大小寫(xiě)s='FDFD'print s.islower()print s.isupper()# s.isspace 判斷是否為 空格s=' 'print s.isspace()# s.upper() s.lower() 大小寫(xiě)互換s='sdgsdgsJLKF'print s.upper()#字符串查找 s.startswith() s.endswith() 返回 bool值s='www.hxend.com's1='www'print s.startswith(s1)print s.endswith(s1)# find 函數(shù) s.replace('','') 函數(shù)代替 找到即可替換s='www.hxend.com'if s.find('hxend'): s5=s.replace('hxend','baidu') #不改變?cè)瓉?lái)的值print s5
#字符串分割函數(shù)
s=' abcdef 1515 dhgghl sdjgsjdg hgkjdhgjk g gskdj gjkg kjsd 'print slist1=s.split()print list1print len(list1)# s.strip() 去掉字符串開(kāi)頭 和結(jié)尾的 空格s1=s.strip()print s1#find 函數(shù)返回 出現(xiàn)這個(gè)字符的位置 找不到 返回 -1a= s1.find('c')print as1=list(s1)print s1# s.find('',k) find 從k開(kāi)始# s.find('') find 從頭開(kāi)始import maths1=s.strip()a=s1.find(' ')print s1[:a]list11=s1.split()lens=len(list11)print format(lens,'2d') #format 的用法i=0for n in range(0,lens-1): #動(dòng)態(tài)變化次數(shù) while s1[a] == ' ': a=a+1 b=s1.find(' ',a) if b!=-1: #考慮到后面結(jié)尾部分 不存在空格了 print s1[a:b] else: print s1[a:] a=b'''count = 5 #這里也是動(dòng)態(tài)變化次數(shù)的方法之一for i in list1: if count == 0: pass else # do sth here... count = count - 1 #'''
#字符串分割函數(shù)的實(shí)現(xiàn)
s='sdgsdg..sdg.f.gs.dh.sd..fsd.g.sd's1=s.split('.') #分割出字符串 去除'.' 然后錄入到列表中去print s1# s.find('',k) find 從k開(kāi)始# s.find('') find 從頭開(kāi)始# s.find('',k) find 從k開(kāi)始# s.find('') find 從頭開(kāi)始import mathdef my_split(sep): s1=s.strip() a=s1.find(sep) print s1[:a] list11=s1.split(sep) lens=len(list11) print format(lens,'2d') #format 的用法 i=0 for n in range(0,lens-1): #動(dòng)態(tài)變化次數(shù) while s1[a] == sep: a=a+1 b=s1.find(sep,a) if b!=-1: #考慮到后面結(jié)尾部分 不存在空格了 print s1[a:b] else: print s1[a:] a=b my_split('.')
list2=[2,32,32,23,232,3,2]list2.append(1) #append() 增加在尾部print list2
adict={'mother':45,'father':55}print adictadict['grandfather']=87 #增加在首部print adictprint adict.keys() # 名字print adict['father'] #提取內(nèi)容#print 默認(rèn)為每個(gè)輸出 輸出一個(gè)換行 后面加上一個(gè) , 可以改變這個(gè)狀態(tài)a='sdgsdgsdsdh'for i ,ch in enumerate(a): #enumrate() 函數(shù) print ch,i
sqdEvens = [x ** 2 for x in range(8) if not x % 2]print sqdEvens`'''handle = open(file_name, access_mode = 'r')file_name 變量包含我們希望打開(kāi)的文件的字符串名字, access_mode 中 'r' 表示讀取,'w' 表示寫(xiě)入, 'a' 表示添加。'''file1=open('second.py','r') #文件操作print file1.readline()#print file1.read()a=file1.readlines() #readlines() 自動(dòng)將文件內(nèi)容分析成一個(gè)行的列表,#該列表可以由 Python 的 for ... in ... 結(jié)構(gòu)進(jìn)行處理print a[1]
關(guān)于作者:[作者]: ——石頭——熱愛(ài)互聯(lián)網(wǎng)事業(yè),關(guān)注互聯(lián)網(wǎng)技術(shù)發(fā)展,文章歡迎轉(zhuǎn)載,請(qǐng)保留原文地址,謝謝。 |
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注