Python

1.list--列表

(1)append:

(2)count:

>>> x = ['to','bee','or','not','to']
>>> x.count('to')
2
>>> x.count('apple')
0

(3)extend:列表尾部一次性追加另一个序列

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>>

(4)index:

>>> x=[1,2,3,4,5]
>>> x.index(2)
1
>>>

(5)insert

>>> x=[1,2,3,5,6,7]
>>> x.insert(3,'four')
>>> x
[1, 2, 3, 'four', 5, 6, 7]
>>>

(6)pop

>>> x=[1,2,3,4,5]
>>> x.pop(1)
2
>>> x
[1, 3, 4, 5]
>>> x.pop()
5
>>> x
[1, 3, 4]
>>>

(7)remove:

>>> x=['to','be','or','not','to','be']
>>> x.remove('to')
>>> x
['be', 'or', 'not', 'to', 'be']
>>>

(8)sort:

>>> x=['aoinog','seiog','lala']
>>> x.sort(key=len)
>>> x
['lala', 'seiog', 'aoinog']
>>>

 

>>> x
['lala', 'seiog', 'aoinog']
>>> x.sort(reverse=True)
>>> x
['seiog', 'lala', 'aoinog']
>>>

 

2.tuple--元组

。。。

3.字符串:

(1)find

>>> s = '$$$ get rich! $$$'
>>> s.find('$$')
0
>>> s.find('$$',3)
14
>>> s.find('$$',1,10)
1

(2)lower

(3)replace

>>> s='This is good!'
>>> b = s.replace('is','are')
>>> b
'Thare are good!'
>>> s
'This is good!'
>>>

(4)split

(5)strip

>>> s='** * spamei gonsg !! **'
>>> s.strip(' !*')
'spamei gonsg'
>>>

(6)translate

>>> from string import maketrans
>>> table=maketrans('cs','lalallala')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: maketrans arguments must have same length
>>> table=maketrans('cs','la')
>>> 'this is an incredible test'.translate(table)
'thia ia an inlredible teat'
>>>

4.字典

格式化:

>>> phontbook={'Beth':'9102','Alice':'2341','Cecil':'3258'}
>>> "Beth phone is %(Beth)s"%phontbook
'Beth phone is 9102'
>>>

(1)clear

(2)copy:浅拷贝

>>> x={'name':'admin','machin':['foo','bar','baz']}
>>> y = x.copy()
>>> y
{'machin': ['foo', 'bar', 'baz'], 'name': 'admin'}
>>> y['name']='song'
>>> y['machin'].remove('baz')
>>> y
{'machin': ['foo', 'bar'], 'name': 'song'}
>>> x
{'machin': ['foo', 'bar'], 'name': 'admin'}
>>>

深拷贝:

>>> from copy import deepcopy
>>> x={'name':'admin','machin':['foo','bar','baz']}
>>> y = x.deepcopy()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'deepcopy'
>>> y = deepcopy(x)
>>> y['machin'].remove('foo')
>>> y
{'machin': ['bar', 'baz'], 'name': 'admin'}
>>> x
{'machin': ['foo', 'bar', 'baz'], 'name': 'admin'}
>>>

 

(3)fromkeys

(4)get

(5)has_key

(6)items iteritems

>>> x={'name':'admin','machin':['foo','bar','baz']}
>>> x.items()
[('machin', ['foo', 'bar', 'baz']), ('name', 'admin')]
>>>
>>> x
{'machin': ['foo', 'bar', 'baz'], 'name': 'admin'}
>>> it = x.iteritems()
>>> it
<dictionary-itemiterator object at 0x106f260a8>
>>>

(7)keys iterkeys

(8)pop

(9)setdefault

(10)update

(11)values itervalues

 

posted @ 2013-12-25 14:31  画家与我  阅读(295)  评论(0编辑  收藏  举报