標籤:資料類型 update equal list in place 出現 method string min
資料類型數字
引號: 123 數值 ‘123‘ 字串 整數:ini long 範圍:(-2**31 - 2**31) num = 123 長整型 long (L) num = 123L浮點型:float複數型:
序列
字串、列表、元組都屬於序列
序列的兩個主要特點是索引操作和切片操作
- 索引操作可以從序列中抓取一個特定的項目
- 切片操作可以從序列中擷取一個切片,即序列的一部分
序列的基本操作
- len() : 求序列的長度
-
-
- in : 判斷元素是否在序列中
- max() ; 返回最大值
- min() ; 返回最小值
- cmp(x,y): 比較兩個序列是否相等
字串 str
字串: str = ‘this is a string‘ 單引號 str = "this is a strting" 雙引號 str = ‘‘‘this is a string‘‘‘ 三引號 str = """ this is a strtng """ 單雙引號在python下沒有區別 三重引號 除了能定義字串(可包含分行符號) 還可以用作注釋
切片
適用於所有序列
```
In [9]: a = ‘abcdef‘
In [10]: a[0:2]
Out[10]: ‘ab‘
取下標0到2 下標為2的不顯示
In [11]: a[:2]
Out[11]: ‘ab‘
取下標0到2 下標為2的不顯示
In [12]: a[:-1]
Out[12]: ‘abcde‘
取下標0到倒數第1個 下標為倒數第1個的不顯示
In [13]: a[::2]
Out[13]: ‘ace‘
步長為2,即取下標為0、2、4、6
In [14]: a[-3:-1]
Out[14]: ‘de‘
從倒數第3個取到倒數第1個,倒數第1個不顯示
In [15]: a[-1:-4:-1]
Out[15]: ‘fed‘
反向切片,從倒數第一個取到倒數第4個,倒數第4個不顯示
In [16]: a[-1:-4:-2]
Out[16]: ‘fd‘
反向切片,從倒數第一個取到倒數第4個,步長為2
```
總結:
a = "abcdef"
a[A:B:C]A:切片開始的下標,包含B:切片結束的下標,不包含C:正數時表示步長 負數時表示進行反向切片及步長
元組 tuble
- 元組和字串一樣是不可變的
- 元組可以儲存一系列的值
- 元組通常肜在使用者定義的函數能夠安全的採用一組值的時候,即被使用的元組的值不會改變
定義元組;
t = tuble() 定義一個元組 多個元素用 ,號分格,元素可以是數字、字串、列表、元組
t = (1,) 當只有一個元素時,需要加一個 ,號,不加則賦值為數字
t = () 定義一個空的元組
元組的拆分:
t = (1, 2, 3)
a, b, c = t 將元組的值 1, 2, 3 依次賦值給 a, b, c
方法:
count() 統計元素在元組中的個數
index() 返回元素中元素第一個出現在下標
列表 list
- 列表是處理一組有序項目的資料結構,即可以在列表中儲存一個序列的項目
- 列表是可變類型的資料
- 建立列表
list1 = [] 建立一個空列表
list2 = list() 使用list方法
list3 = [‘a‘, 1, [‘a‘, 1]]
方法:
append() 追加
L.append(object) -- append object to end
del list[n] 根據下標刪除某個元素
remove() 刪除列表裡第一個出現在元素
L.remove(value) -- remove first occurrence of value.
insert() 在某個場索引前插入一個元素
L.insert(index, object) -- insert object before index
sort() 給列表陫序
L.sort(cmp=None, key=None, reverse=False) -- stable sort IN PLACE;
cmp(x, y) -> -1, 0, 1
reverse() 反轉
L.reverse() -- reverse *IN PLACE*
pop() 刪除並返回指定下標的元素值,不指定預設刪除最後一個
L.pop([index]) -> item -- remove and return item at index (default last).
extend() 通過附加迭代元素來擴充列表
extend list by appending elements from the iterable
字典
- 字典是python中唯一映射的類型(雜湊表)
- 字典對象是可變的,但字典的鍵必須是不可變的對象,一個字典中可以使用不同類型的索引值
- 建立字典
dict1 = {} 使用 {} 建立一個空字典
dict2 = {‘name‘:‘li‘, ‘age‘:10}
dict3 = dict([(‘name‘,‘li‘),(‘age‘, 10)])
dict4 = dict(‘a‘=10, ‘n‘=100)
字典的方法:
keys() 擷取所有key
D.keys() -> list of D‘s keys
- values() 擷取所有 values
D.values() -> list of D‘s values
get() 擷取key對應的值,不存在時可指定輸出,預設為空白
D.get(k[,d]) -> D[k] if k in D, else d. d defaults to None.
has_key() 查看key是否存在
D.has_key(k) -> True if D has a key k, else False
iitems() 轉換為以(key, value)組成的列表
D.items() -> list of D‘s (key, value) pairs, as 2-tuples
copy() 拷備
D.copy() -> a shallow copy of D
clear() 清空字典
D.clear() -> None. Remove all items from D
pop() 刪除指定KEY的value並返回value,如果KEY不存在可以返回一個給定value,否則拋出導常
D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
update() 從字典或兩個迭代對象更新字典
D.update([E, ]**F) -> None. Update D from dict/iterable E and F.
If E present and has a .keys() method, does: for k in E: D[k] = E[k]
If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v
In either case, this is followed by: for k in F: D[k] = F[k]
fromkeys() 使用序列產生一個字典,值預設為空白,可指定
dict.fromkeys(S[,v]) -> New dict with keys from S and values equal to v.
v defaults to None.
python基礎複習-1-2 資料類型-str、list、tuple、dict