Python常用的內建序列結構(列表、元組、字典)學習筆記,python學習筆記

來源:互聯網
上載者:User

Python常用的內建序列結構(列表、元組、字典)學習筆記,python學習筆記

列表與元組
列表用大括弧[]表示,元組用圓括弧()表示。
列表可以修改,字串與元組不可修改。
元組的分區還是元組,列表的分區還是列表。

1.列表方法:

name=["zhang3","li4","wang5"]name.append("gou6") #添加項name.remove("gou6") #移除第一個匹配項,也可用del name[3]來移除name.insert(3,"gou6") #插入項name.index("gou6") #找出第一個匹配項的位置name.extend(["gou6","xuan7"]) #擴充name.pop(0) #返回列表的第一項值並從列表中刪除之

2.列表函數:

>>> a=list("hi guys") #把字串轉換為列表>>> print a['h', 'i', ' ', 'g', 'u', 'y', 's']>>> ''.join(a)  #把列表還原成字串'hi guys'>>> max(a) #取得列表的最大元素'y'>>> len(a) #取得列表長度7>>> min(a) #取得最小元素' '>>> tuple(a) #將列錶轉換為元組('h', 'i', ' ', 'g', 'u', 'y', 's')>>> sorted(a) #將列表元素排序[' ', 'g', 'h', 'i', 's', 'u', 'y']

3.列表遍曆:

A,使用for語句遍曆

for each_item in name:   print(each_item)

B,使用while語句遍曆

i=0while i < len(name):   print(name[i])   i += 1

4.成員資格1:

>>> sub="hello, you are a bear">>> "bear" in subTrue>>> "y" in subTrue>>> raw_input("what's your name?") in subwhat's your name?bearTrue

5.成員資格2:

database=[["zhang3","0111"],["li4","0112"],["wang5","0113"]]username=raw_input("what's your user name?")id=raw_input("what's your id?")if [username,id] in database: print "access granted"

6.找出10以內的整數

s = [x for x in range(0, 10) if x % 2 == 0]

7.產生九九乘法表

s = [(x, y, x*y) for x in range(1, 10) for y in range(1,10) if x>=y]

字串

1.擷取字串 

name=raw_input("what's your name?")      print "Hello," + name + ".welcome to us"

注意:Pyhton3.x版本取消了raw_input,統一使用input
輸出值:

print name + repr(x)#str用於把值轉換為合理的字串,repr建立一個字串,傳回值的字串形式#str是一種類型(和int一樣),repr是函數

2.分行符號用\n表示
原始字串,以字串前加一個r即可,如

print r"c:\nowindows\no"path="c:\nowindows\no"; print repr(path)

3.Unicode字串

 print u"redhat"

注意:Pyhton3.x版本所有字串都是unicode字串
定義字串時,雙引號和單引號都是可以用的,只不過用單引號的時候可以在字串裡面使用雙引號
布爾值:

>>> bool('i love you')True>>> bool(42)True>>> bool(1)True>>> bool('0')True>>> bool(0)False>>> bool('')False

4.字串方法

>>> tag="<a href=http://www.baidu.com>baidu indexpage</a>">>> print tag[8:28] #字串分區http://www.baidu.com>>> print tag[29:-4] #字串分區baidu indexpage>>> tag.replace("www.baidu.com","home.sina.com") #字串替換'<a href=http://home.sina.com>baidu indexpage</a>'>>> dirs=["","usr","bin","env"]>>> "/".join(dirs)  #將列表拼接成字串'/usr/bin/env'>>> print ("C:" + "\\".join(dirs))C:\usr\bin\env>>> path="/usr/bin/env">>> path.split("/") #將字串分割成列表['', 'usr', 'bin', 'env']

5.其它字串方法

>>> s=' I Love you!  '>>> s.lower() #轉換字串的小寫' i love you!  '>>> s.upper() #轉換字串的大寫' I LOVE YOU!  '>>> s.title() #換換字串為標題(所有單字首大寫)' I Love You!  '>>> s.islower() #判斷字串是否為小寫(也可判斷大寫和標題)False>>> s.strip()  #去除首尾空格,lstrip去除左邊空格,rstrip去除右邊空格'I Love you!'>>> word=s.split() #分割>>> word['I', 'Love', 'you!']>>> '::'.join(word) #合并'I::Love::you!'>>> s.count('o') #統計出現次數2>>> s.find('you') #尋找位置,如果找不到,則返回-19>>> s.startswith('python')False>>> s.replace('you','yours')' I Love yours!  '

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.