Python(字符串,列表,元组,字典)
1.字符串:標識" "
PS:字符串是%s;整數是%d;浮點數%f
strip:移除(空白或者特定字符)
split:分割
len:長度(獲取元素的個數)
索引
切片
2.列表list:方括號[ ]
PS:先了解
- 序列是Python中最基本的數據結構,Python有6個序列的內置類型,但最常見的是列表和元組。序列都可以進行的操作包括索引,切片,加,乘,檢查成員。
- 列表是最常用的Python數據類型
刪除list末尾元素,用pop():
>>> classmates.append('Tom') >>> print classmates ['john', 'jack', 'Tom'] >>> classmates.pop() 'Tom'刪除指定位置元素,用pop(i):
>>> print classmates ['john', 'jack', 'Tom'] >>> classmates.pop() 'Tom' >>> classmates.pop(0) 'john'替換某個元素,直接賦值給對應的索引位置:例子省略
list 元素也可以是另一個list,它只是表示一個元素:
>>> s=['python','java',['c','c++'],'php'] >>> len(s) 4接著要拿到' c '可以寫成s[2][1],因此s可以看成一個二維數組,類似三維,四位
空的list,長度為0:
>>> L=[] >>> len(L) 0python 創建二維列表,將需要的參數寫入 cols 和 rows 即可:
#創建二維列表:list_2d = [[0 for col in range(cols)] for row in range(rows)] list_2d=[[0 for i in range(5)]for i in range(6)] print list_2d list_2d[0].append(3) list_2d[0].append(5) list_2d[2].append(8) print list_2d View Code列表排序:
#list.sort() 對列表元素排序,數字的話從小到大 >>> a=[1,4,3,4,7,8] >>> a.sort() >>> a [1, 3, 4, 4, 7, 8]#list.reverse() 對列表元素進行倒序 >>> a=[1,4,3,4,7,8] >>> a.reverse() >>> a [8, 7, 4, 4, 3, 1] View Code?
3.元組tuple:小括號()
PS:元組不能二次賦值,相當于只讀列表。即元組的元素不能修改
tuple = ( 'runoob', 786 , 2.23, 'john', 70.2 )list = [ 'runoob', 786 , 2.23, 'john', 70.2 ]tuple[2] = 1000 # 元組中是非法應用 list[2] = 1000 # 列表中是合法應用 View Code #創建空元組 tup1=() #元組中只包含一個元素時,需要在元素后面添加逗號,因為括號()既可以表示tuple,又可以表示數學公式中的小括號,這就產生了歧義 tup1=(50,)注意:不可變指的是tuple的元素,tuple所謂的“不變”是說,tuple的每個元素,指向永遠不變。
>>> t=('a','b',['A','B']) >>> t[2][0]='X' >>> t[2][1]='Y' >>> t ('a', 'b', ['X', 'Y']) #一個“可變”的tuple,實際上是list的元素變了輸出:
tuple=('runoob',78,2.23,'john',70.2) tinytuple=(123,'john') print tuple print tuple[0] print tuple[1:3] #輸出第二個至第三個的元素 print tuple[2:] #輸出從第三個開始至列表末尾的元素 print tinytuple*2 print tuple+tinytuple View Code訪問元組:
#!/usr/bin/env python # -*- coding:utf-8 -*- tup1=('physics','chemistry',1990,2001) tup2=(1,2,3,4,5)print "tup1[0]:",tup1[0] print "tup2[1:4]:",tup2[1:4] View Code元組元素值不能修改,但能進行元組連接組合:
tup1=(12,23,34) tup2=('abc','xyz') #創建一個新的元組,進行連接組合 tup3=tup1+tup2 print tup3 View Code元組不能增加元素,沒有append和extend方法
元組不能刪除元素,tuple沒有remove或pop,但可使用del語句來刪除整個元組:
tup=('abc','john',123,446) print tup del tup print "After deleting tup:" print tup View Code?迭代:
tup=(1,2,3) for i in tup:print i View Code計算元素個數:
tup=(2,3,4,5,6,7) print len(tup) View Code索引:
L=('a','b','c','d') print L[-2] #反向讀取,讀取倒數第二個元素:c View Code無關閉分隔符:任意無符號的對象,以逗號隔開,默認為元組,如:
print 'abc',-4.2e21,18+6j,'xyz' x,y=1,2 print "Value of x,y:",x,y View Code比較兩個元組:cmp(tup1,tup2)
返回元組中元素最大值,最小值:max(tup); min(tup)
將列表轉換為元組:tuple(seq)
print tuple([1,2,3,4])print tuple({1:2,3:4}) #針對字典 會返回字典的key組成的tuple:(1,3)#元組會返回元組本身 View Code?
4.字典dict:花括號{ }
組成:由索引(key)和它對應的值value組成,格式:d = {key1 : value1, key2 : value2 }
字典是另一種可變容器模型,且可存儲任意類型對象。字典是除列表以外python之中最靈活的內置數據結構類型。列表是有序的對象集合,字典是無序的對象集合。
鍵必須是唯一的,但值則不必。
兩者之間的區別在于:字典當中的元素是通過鍵來存取的,而不是通過偏移存取。
?創建:
dict1={'abc':111,'Age':37} View Code訪問字典中的值:(如果用字典里沒有的鍵訪問數據,會輸出錯誤)
dict={'Name':'john','Age':7,'Class':'First'} print "dict['Name']:",dict['Name'] print "dict['Age']:",dict['Age'] View Code修改字典:
#修改字典的方法是增加新的鍵/值對,修改或刪除已有的鍵/值對 dict={'Name':'john','Age':7,'Class':'First'} dict['Age']=8 #修改 dict['School']='Yi Ling School' #增加del dict['Name'] #刪除鍵為'Name'的條目 print dict dict.clear() #清除dict字典的所有條目 print dict del dict print dict View Code字典鍵的特性:
- 不允許同一個鍵出現兩次。創建時如果同一個鍵被賦值兩次,后一個值會被記住:
View Code
- 鍵必須不可變,所以可以用數字,字符串或元組充當,所以用列表就不行:
View Code
5.set:? set和dict類似,也是一組key的集合,但不存儲value。由于key不能重復,所以,在set中,沒有重復的key
1.要創建一個set,需要提供一個list作為輸入集合:
>>> a=set([1,2,3])
>>> a
set([1, 2, 3])
轉載于:https://www.cnblogs.com/zhou0000/p/8502850.html
總結
以上是生活随笔為你收集整理的Python(字符串,列表,元组,字典)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Apache Solr Velocity
- 下一篇: python中列表 字典 元祖 enum