1.清單
a = ['a', 'b', 'c', 'abc']
## append 末尾追加
a.append('hello')
print (a)
['a', 'b', 'c', 'abc', 'hello']
## pop 末尾删除
a.pop()
['a', 'b', 'c']
## index 索引
print(a[0],a[1])
('a', 'b')
print (a.index('b'))
1
## insert 插入
a.insert(0, 'ajing')
print a
['ajing', 'a', 'b', 'c', 'abc']
a.insert(3, 'lili')
['a', 'b', 'c', 'lili', 'abc']
## remove 删除(一次隻能删除最前面的一個)
a.remove('abc')
## sort 排序
a.sort()
['a', 'abc', 'b', 'c']
## reverse 反序
a.reverse()
['abc', 'c', 'b', 'a']
2. 切片
## 列印a所有元素
print a[:]
['a', 'b', 'c', 'abc']
## 列印1到最後
print a[1:]
['b', 'c', 'abc']
## 列印1-2
print a[1:3]
['b', 'c']
備注:切片[1:3]取到的最後一位數字,是[1:3]最後一位數字(3)減1
## 間隔列印(步長)
b = range(10)
print b[0:7:2]
[0, 2, 4, 6]
## 反向切片
print a[-1]
print a[-4]
print a[:-1]
print a[-3:]
結果:
abc
a
3.元組
清單和元組很相似,清單是可變的,元組是不可變的
## 字元串轉換成元組:
str1 = 'abcdefghigklmn'
print (tuple(str1))
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n')
## 元組 a
a = ('a', 'b', 'c', 'abc', 'hello')
單個元組的時候要注意加逗号:
b = ('mn',)
print (type(b))
<type 'tuple'> (類型是元組)
否則python解析器不會識别為 tuple 類型。
## 元組方法:
count 和 index
### count 統計指定元組内相同元素的個數
c = ('a', 'b', 'c', 'a', 'a','k','e')
print a.count('a')
3 (個)
### index 索引(傳回某個元素的下标)
print c.index('k')
5 (第5個位置)
注:索引的元素不在元組内的時候報 ValueError 的錯誤。
4.字典
字典 同很多語言一樣是 key:value 這種形式
字典是另一種可變容器模型,可存儲任意類型的對象。
字典的每個鍵值對(key => value)用冒号(:)分割,每個對之間用逗号(,)分割,
整個字典包含在{}(大括号)中
字典指派有三種方式:
k = {'name':'tiantian', 'age':'10', 123:'abc'}
d = dict(a=1,b=2,c=3)
e = dict([('name','lie'),('age',20)])
字典有很多常用的方法:
## clear 清空字典
e.clear()
## get 擷取對應key的value,值不存在傳回none
print (k.get('name'))
'tiantian'
print (k.get('xxx'))
None
字典中沒有這個key和value,則傳回None
## setdefault 擷取對應的key的value,若值不存在,可以自定義值
print (k.setdefault('name'))
print (k.setdefault('name', 'didi'))
'didi'
print (k.setdefault('xxx'))
print (k.setdefault('xxx', 'beijing'))
'beijing'
## keys 擷取字典中的所有key,并組成清單
print (k.keys)
['age', 123, 'name']
## iterkeys 擷取字典中所有key的對象
print (k.iterkeys())
<dictionary-keyiterator object at 0x00000000026B5F48>
## values 擷取字典中所有value,并組成清單
## itervalues 擷取字典中所有values的對象
print (k.values())
['10', 'abc', 'tiantian']
print (k.itervalues())
<dictionary-valueiterator object at 0x0000000002585F98>
## iteritems 周遊
## items 周遊
print (k.iteritems())
<dictionary-itemiterator object at 0x0000000002705F98>
print (k.items())
[('age', '10'), (123, 'abc'), ('name', 'tiantian')]
【iteritems() 比 items() 更好的利用資源】
for x, y in k.iteritems():
print (x, y)
傳回單個元組:
('age', '10')
(123, 'abc')
('name', 'tiantian')
## pop 删除字典中指定keys以及他的value
k.pop('name')
print k
傳回:{'age': '10', 123: 'abc'}
5.字典的進階操作
操作一:将清單元素賦value後,組成字典
f = ['a', 'b', 'c', 'd']
m = {}
n = m.fromkeys(f, 123) (或者:n = dict.fromkeys(f, 123),同理)
print (n)
傳回:
{'a': 123, 'c': 123, 'b': 123, 'd': 123}
操作二:将兩個清單合為字典
f1= ['a', 'b', 'c', 'd']
f2 = [1, 2, 3, 4]
dict_test = dict(zip(f1, f2))
print (dict_test)
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
操作三:把第一個字典,增加到第二個字典(合并字典)
dict_test.update(k)
print dict_test
{'a': 1, 'c': 3, 'b': 2, 'd': 4, 'age': '10', 123: 'abc', 'name': 'tiantian'}
操作四:對字典進行排序
mm = dict( a=1, b=2, c=3, d=4)
print (mm)
print sorted(mm.itertiems(), key = lambda d:d[0], reverse = True)
【reverse True 表示反序,False 表示正序】
本文轉自 聽丶飛鳥說 51CTO部落格,原文連結:http://blog.51cto.com/286577399/1975340