file_obj2=open('hello.txt','w')conta='my name is Bb'file_obj2.write(conta)v=file_obj2.readlines()PRint v
輸出不唯一數1 (這是一個國外Python練習網站上的題目 我其他博文有介紹 )
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自定義函數預定義 1.預定值放在前面 2. 后面賦值可以把先前預定的值覆蓋#
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語句#
#注意使用 raw_input()的時候 把字符強制轉化成 整形#
count =int(raw_input('input your number:'))print countif count>=80: print 'a'elif count>=70: print'b'else : print 'nono'
#字符區域代碼#
sex=raw_input('put your sex')if sex=='Male': print 'right'else: print 'no'
#python if分支語句表達式構造 非0 即為真#
#關系表達式 > < = 邏輯表達式 and or not
#網絡刷博客瀏覽量#
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 數據類型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 直接用數據也可以 print format(j,'2d'),str; j=j+1;k=0;for k in range(1,101,2): #range 函數 范圍在 start 到 end-1 最后一位為每個數字間隔數 print k;
s1='hello world'
#for循環 循環體遍歷文件和元組#
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) # 在原目錄下面增加一個txt文件 在文件里面寫入打開的文件內容
#轉移字符串print r'hello/nworld' #r關閉轉義字符串的作用print u'unicode' #unicode 字符串#格式化字符串print 'your age is %d'%(28)
# open('c://temp//temp.txt','a+') 用一個‘、’可能被認為是轉義字符
s1='www.hxend's2='.com'print s1,s2 #這樣輸出的話 中間會存在一個空格print s1+s2 #用‘+’連接兩個字符串#字符串的重復 *s='abcdefg'print s*5
#訪問字符串的某個元素 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] #如果發生錯誤 就會出現 空格表示# 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 循環遍歷整個字符串s5='www.hxend.com'for ch in s5: print ch;
#字符串的高級函數
#isalnum 判斷是否為 數字或者為字母 s.isalnum() s='wwwhxend55com'print s.isalnum()# s.isalpha 判斷是否為字母s='abcdef'print s.isalpha() # s.isdigit() 判斷是否為數字s='626611'print s.isdigit()# s.islower s.isupper 判斷大小寫s='FDFD'print s.islower()print s.isupper()# s.isspace 判斷是否為 空格s=' 'print s.isspace()# s.upper() s.lower() 大小寫互換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 函數 s.replace('','') 函數代替 找到即可替換s='www.hxend.com'if s.find('hxend'): s5=s.replace('hxend','baidu') #不改變原來的值print s5
#字符串分割函數
s=' abcdef 1515 dhgghl sdjgsjdg hgkjdhgjk g gskdj gjkg kjsd 'print slist1=s.split()print list1print len(list1)# s.strip() 去掉字符串開頭 和結尾的 空格s1=s.strip()print s1#find 函數返回 出現這個字符的位置 找不到 返回 -1a= s1.find('c')print as1=list(s1)print s1# s.find('',k) find 從k開始# s.find('') find 從頭開始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): #動態變化次數 while s1[a] == ' ': a=a+1 b=s1.find(' ',a) if b!=-1: #考慮到后面結尾部分 不存在空格了 print s1[a:b] else: print s1[a:] a=b'''count = 5 #這里也是動態變化次數的方法之一for i in list1: if count == 0: pass else # do sth here... count = count - 1 #'''
#字符串分割函數的實現
s='sdgsdg..sdg.f.gs.dh.sd..fsd.g.sd's1=s.split('.') #分割出字符串 去除'.' 然后錄入到列表中去print s1# s.find('',k) find 從k開始# s.find('') find 從頭開始# s.find('',k) find 從k開始# s.find('') find 從頭開始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): #動態變化次數 while s1[a] == sep: a=a+1 b=s1.find(sep,a) if b!=-1: #考慮到后面結尾部分 不存在空格了 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'] #提取內容#print 默認為每個輸出 輸出一個換行 后面加上一個 , 可以改變這個狀態a='sdgsdgsdsdh'for i ,ch in enumerate(a): #enumrate() 函數 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 變量包含我們希望打開的文件的字符串名字, access_mode 中 'r' 表示讀取,'w' 表示寫入, 'a' 表示添加。'''file1=open('second.py','r') #文件操作print file1.readline()#print file1.read()a=file1.readlines() #readlines() 自動將文件內容分析成一個行的列表,#該列表可以由 Python 的 for ... in ... 結構進行處理print a[1]
關于作者:[作者]: ——石頭——熱愛互聯網事業,關注互聯網技術發展,文章歡迎轉載,請保留原文地址,謝謝。 |
新聞熱點
疑難解答