Python常用的內(nèi)置序列結(jié)構(gòu)(列表、元組、字典)學(xué)習(xí)筆記
列表與元組
列表用大括號(hào)[]表示,元組用圓括號(hào)()表示。
列表可以修改,字符串與元組不可修改。
元組的分片還是元組,列表的分片還是列表。
1.列表方法:
name=["zhang3","li4","wang5"]
name.append("gou6") #添加項(xiàng)
name.remove("gou6") #移除第一個(gè)匹配項(xiàng),也可用del name[3]來移除
name.insert(3,"gou6") #插入項(xiàng)
name.index("gou6") #找出第一個(gè)匹配項(xiàng)的位置
name.extend(["gou6","xuan7"]) #擴(kuò)展
name.pop(0) #返回列表的第一項(xiàng)值并從列表中刪除之
2.列表函數(shù):
>>> a=list("hi guys") #把字符串轉(zhuǎn)換為列表
>>> print a
['h', 'i', ' ', 'g', 'u', 'y', 's']
>>> ''.join(a) #把列表還原成字符串
'hi guys'
>>> max(a) #取得列表的最大元素
'y'
>>> len(a) #取得列表長(zhǎng)度
7
>>> min(a) #取得最小元素
' '
>>> tuple(a) #將列表轉(zhuǎn)換為元組
('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=0 while i < len(name): print(name[i]) i += 1
4.成員資格1:
>>> sub="hello, you are a bear"
>>> "bear" in sub
True
>>> "y" in sub
True
>>> raw_input("what's your name?") in sub
what's your name?bear
True
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以內(nèi)的整數(shù)
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,統(tǒng)一使用input
輸出值:
print name + repr(x) #str用于把值轉(zhuǎn)換為合理的字符串,repr創(chuàng)建一個(gè)字符串,返回值的字符串形式 #str是一種類型(和int一樣),repr是函數(shù)
2.換行符用\n表示
原始字符串,以字符串前加一個(gè)r即可,如
print r"c:\nowindows\no" path="c:\nowindows\no"; print repr(path)
3.Unicode字符串
print u"redhat"
注意:Pyhton3.x版本所有字符串都是unicode字符串
定義字符串時(shí),雙引號(hào)和單引號(hào)都是可以用的,只不過用單引號(hào)的時(shí)候可以在字符串里面使用雙引號(hào)
布爾值:
>>> 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() #轉(zhuǎn)換字符串的小寫
' i love you! '
>>> s.upper() #轉(zhuǎn)換字符串的大寫
' I LOVE YOU! '
>>> s.title() #換換字符串為標(biāo)題(所有單詞首字母大寫)
' I Love You! '
>>> s.islower() #判斷字符串是否為小寫(也可判斷大寫和標(biāo)題)
False
>>> s.strip() #去除首尾空格,lstrip去除左邊空格,rstrip去除右邊空格
'I Love you!'
>>> word=s.split() #分割
>>> word
['I', 'Love', 'you!']
>>> '::'.join(word) #合并
'I::Love::you!'
>>> s.count('o') #統(tǒng)計(jì)出現(xiàn)次數(shù)
2
>>> s.find('you') #查找位置,如果找不到,則返回-1
9
>>> s.startswith('python')
False
>>> s.replace('you','yours')
' I Love yours! '
相關(guān)文章
Python3中的列表,元組,字典,字符串相關(guān)知識(shí)小結(jié)
這篇文章主要介紹了Python3中的列表,元組,字典,字符串相關(guān)知識(shí)小結(jié),小編覺得挺不錯(cuò)的,分享給大家,需要的朋友可以參考下。2017-11-11
Python使用colorlog實(shí)現(xiàn)控制臺(tái)管理日志多種顏色顯示
colorlog 是一個(gè) Python 日志庫,它可以讓你在控制臺(tái)中以彩色的方式顯示日志消息,使得日志更易于閱讀和理解,下面就跟隨小編一起來看看它的具體應(yīng)用吧2024-03-03
Python數(shù)據(jù)處理-導(dǎo)入導(dǎo)出excel數(shù)據(jù)
這篇文章主要介紹了Python數(shù)據(jù)處理-導(dǎo)入導(dǎo)出excel數(shù)據(jù),Python的一大應(yīng)用就是數(shù)據(jù)分析了,而數(shù)據(jù)分析中,經(jīng)常碰到需要處理Excel數(shù)據(jù)的情況。這里做一個(gè)Python處理Excel數(shù)據(jù)的總結(jié),需要的小伙伴可以參考一下2022-01-01

