每天学习一点点,通过例子

字符串

>>> word='asdf'

>>> word[:2]

'as'

>>> word[1:]

'sdf'

>>> a= [x for x in 'abcdsf' if x not in 'abc']

>>> a

['d', 's', 'f']


链表

以[]

ques = ['a','b','c']

 ans = ['1','2','3']

for q,a in zip(ques,ans)

print q,a


字典


>>> person ={'name':'xiaoli','age':21,'sex':'male'}

>>> person

{'age': 21, 'name': 'xiaoli', 'sex': 'male'}

>>> person['name']

'xiaoli'

>>> person['name'] = 'xiaohong'

>>> person

{'age': 21, 'name': 'xiaohong', 'sex': 'male'}

>>> del person['sex']

>>> person

{'age': 21, 'name': 'xiaohong'}

>>> list(person.keys())

['age', 'name']

>>> for k,v in person.items():

...     print (k,v)

... 

('age', 21)

('name', 'xiaohong')

元组

以,区分,元组是不变的

>>> a =123,456,'asd'

>>> a

(123, 456, 'asd')

>>> a[0]

123

>>> b = a,'ertr','dfg'

>>> b

((123, 456, 'asd'), 'ertr', 'dfg')

>>> c = ([1,2,3],[3,2,1])

>>> c

([1, 2, 3], [3, 2, 1])



链表几个方法append,extend,insert,remove,del,pop的的的用法。元组不可用

>>> mem=['a','b']

>>> mem

['a', 'b']

>>> mem.append('c')

>>> mem

['a', 'b', 'c']

>>> mem.extend(['d','f'])

>>> mem

['a', 'b', 'c', 'd', 'f']

>>> mem.insert(1,'g')

>>> mem

['a', 'g', 'b', 'c', 'd', 'f']

>>> mem.remove('a')

>>> mem

['g', 'b', 'c', 'd', 'f']

>>> mem.pop()

'f'

>>> mem

['g', 'b', 'c', 'd']